A store needs to keep several items in one shopping basket.
What You Need to Understand
A collection holds multiple related values under one name. Lists, tuples, dictionaries and sets answer different questions: order, mutability, key-based lookup and uniqueness. Choosing a suitable collection simplifies later code.
When to Use It
A store needs to keep several items in one shopping basket.
Step-by-Step Python Example
single_item = "Notebook"
items = ["Notebook", "Pen", "Folder"]
print(single_item)
print(items)
print(len(items))
How the Example Works
The list groups three ordered values, allowing one loop or operation to work on the whole basket.
Example Output
Notebook
['Notebook', 'Pen', 'Folder']
3
19
22
21
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
temperatures = [19, 22, 21]
for temperature in temperatures:
print(temperature)
Important Behavior and Edge Cases
Collections do not always imply the same ordering or mutability. A list is not a set, and a dictionary is not indexed like a sequence.
Try It Yourself
Add an item and print every basket entry using a loop.
Common Beginner Mistakes
- Collections do not always imply the same ordering or mutability. A list is not a set, and a dictionary is not indexed like a sequence.
- Assuming every sequence operation modifies the original data or that a tuple makes nested mutable objects immutable.
Key Takeaways
- A collection holds multiple related values under one name.
- The list groups three ordered values, allowing one loop or operation to work on the whole basket.
- Collections do not always imply the same ordering or mutability.