Remember
Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.
Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = ["tea"] * 3
unique_items = set(items)
print(len(items))
print(len(unique_items))Top-level scope
['tea', 'tea', 'tea']Object 2 · set{'tea'}Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = ["tea"] * 3
unique_items = set(items)
print(len(items))
print(len(unique_items))
|
v
DISPLAYED RESULT
3
1
FINAL BINDINGS
items --> object 1 (list)
unique_items --> object 2 (set)
object 1: ['tea', 'tea', 'tea']
object 2: {'tea'}Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = ["tea"] * 3
unique_items = set(items)
print(len(items))
print(len(unique_items))Top-level scope
['tea', 'tea', 'tea']Object 2 · set{'tea'}The example has finished. Compare the final output with the values and references above.
3 1
What do you expect this program to print?
items = ["tea"] * 3
unique_items = set(items)
print(len(items))
print(len(unique_items))In Python, collections are ways to store multiple values in a single variable. Imagine a box where you can keep many items together – that’s what collections help you do in programming.
Python has four main types of collections:
Lists let you store multiple items in one variable. You can change, add, or remove items.
fruits = ["apple", "banana", "cherry"]
print(fruits)
['apple', 'banana', 'cherry']
Why this output? The list keeps all three fruits in the order they were added. The square brackets [] show it’s a list.
Tuples are like lists, but you can’t change them once created.
colors = ("red", "green", "blue")
print(colors)
('red', 'green', 'blue')
Why this output? The items are inside parentheses (), showing it’s a tuple. The order is kept, but the values can’t be changed later.
Sets store items without any duplicates and without keeping order.
numbers = {1, 2, 3, 2, 1}
print(numbers)
{1, 2, 3}
Why this output? Sets remove duplicates automatically. The numbers 2 and 1 appeared twice but are shown only once. The order may vary each time you run the code.
Dictionaries store data in a key: value format. Each key is unique and helps you find its value.
student = {"name": "Alice", "age": 20}
print(student)
{'name': 'Alice', 'age': 20}
Why this output? The dictionary keeps two key-value pairs. The keys "name" and "age" point to their respective values.
| Type | Ordered? | Changeable? | Duplicates Allowed? | Syntax |
|---|---|---|---|---|
| List | Yes | Yes | Yes | [ ] |
| Tuple | Yes | No | Yes | ( ) |
| Set | No | Yes | No | { } |
| Dictionary | No (but keys are read in insertion order) | Yes | No (keys must be unique) | { key: value } |
Connect what you noticed in the example with the idea below.
Collections group values. Lists keep a sequence, dictionaries associate keys with values, and sets keep distinct elements.
A set preserves duplicate elements.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.