0. What is a Collection?
A collection is a way to store many values inside one variable.
Without collections, we may write:
student1 = "Amit"
student2 = "Riya"
student3 = "Karan"
print(student1)
print(student2)
print(student3)
With a collection, we can write:
students = ["Amit", "Riya", "Karan"]
print(students)
1. List
A list is an ordered collection of values. Lists are written using square brackets:
[].
marks = [80, 75, 90]
Features of List
- Ordered
- Changeable
- Allows duplicate values
- Uses square brackets:
[]
List Example 1: Student Names
students = ["Amit", "Riya", "Karan"]
print(students)
print(students[0])
print(students[1])
print(students[2])
['Amit', 'Riya', 'Karan']
Amit
Riya
Karan
students[0] means the first item.
List Example 2: Add and Change Values
fruits = ["apple", "banana", "mango"]
print("Original list:", fruits)
fruits.append("orange")
print("After adding orange:", fruits)
fruits[1] = "grapes"
print("After changing banana to grapes:", fruits)
Original list: ['apple', 'banana', 'mango']
After adding orange: ['apple', 'banana', 'mango', 'orange']
After changing banana to grapes: ['apple', 'grapes', 'mango', 'orange']
2. Tuple
A tuple is also an ordered collection. Tuples are written using round brackets:
().
point = (10, 20)
Features of Tuple
- Ordered
- Not changeable
- Allows duplicate values
- Uses round brackets:
()
Tuple Example 1: Coordinates
point = (10, 20)
print(point)
print("x =", point[0])
print("y =", point[1])
(10, 20)
x = 10
y = 20
Tuple Example 2: Days of Week
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
print(days)
print(days[0])
print(days[4])
('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
Monday
Friday
What happens if we try to change a tuple?
days = ("Monday", "Tuesday", "Wednesday")
days[0] = "Sunday"
TypeError: 'tuple' object does not support item assignment
3. Set
A set is a collection of unique values. Sets are written using curly brackets:
{}.
numbers = {1, 2, 3}
Features of Set
- Unordered
- Changeable
- Does not allow duplicate values
- Uses curly brackets:
{}
Set Example 1: Remove Duplicates Automatically
numbers = {1, 2, 3, 2, 1, 4}
print(numbers)
{1, 2, 3, 4}
Set Example 2: Add and Remove Items
subjects = {"Python", "Java", "SQL"}
print("Original set:", subjects)
subjects.add("HTML")
print("After adding HTML:", subjects)
subjects.remove("Java")
print("After removing Java:", subjects)
Original set: {'Python', 'Java', 'SQL'}
After adding HTML: {'Python', 'Java', 'SQL', 'HTML'}
After removing Java: {'Python', 'SQL', 'HTML'}
4. Frozenset
A frozenset is like a set, but it cannot be changed.
numbers = frozenset([1, 2, 3])
Features of Frozenset
- Unordered
- Not changeable
- Does not allow duplicate values
- Created using
frozenset()
Frozenset Example 1: Create a Frozenset
numbers = frozenset([1, 2, 3, 2, 1])
print(numbers)
frozenset({1, 2, 3})
Frozenset Example 2: Check Membership
subjects = frozenset(["Python", "Java", "SQL"])
print("Python" in subjects)
print("HTML" in subjects)
True
False
What happens if we try to add to a frozenset?
subjects = frozenset(["Python", "Java", "SQL"])
subjects.add("HTML")
AttributeError: 'frozenset' object has no attribute 'add'
5. Dictionary
A dictionary stores data in key-value pairs.
student = {
"name": "Amit",
"age": 15,
"marks": 88
}
Each item has a key and a value:
"name": "Amit"
"name"is the key"Amit"is the value
Features of Dictionary
- Ordered
- Changeable
- Does not allow duplicate keys
- Stores key-value pairs
Dictionary Example 1: Student Data
student = {
"name": "Amit",
"age": 15,
"marks": 88
}
print(student)
print(student["name"])
print(student["age"])
print(student["marks"])
{'name': 'Amit', 'age': 15, 'marks': 88}
Amit
15
88
Dictionary Example 2: Add and Update Values
student = {
"name": "Riya",
"age": 14,
"marks": 91
}
print("Original dictionary:", student)
student["city"] = "Varanasi"
print("After adding city:", student)
student["marks"] = 95
print("After updating marks:", student)
Original dictionary: {'name': 'Riya', 'age': 14, 'marks': 91}
After adding city: {'name': 'Riya', 'age': 14, 'marks': 91, 'city': 'Varanasi'}
After updating marks: {'name': 'Riya', 'age': 14, 'marks': 95, 'city': 'Varanasi'}
Comparison Table
| Collection | Brackets / Syntax | Ordered | Changeable | Allows Duplicates | Example |
|---|---|---|---|---|---|
| List | [] |
Yes | Yes | Yes | [1, 2, 2, 3] |
| Tuple | () |
Yes | No | Yes | (1, 2, 2, 3) |
| Set | {} |
No | Yes | No | {1, 2, 3} |
| Frozenset | frozenset() |
No | No | No | frozenset([1, 2, 3]) |
| Dictionary | {key: value} |
Yes | Yes | Keys cannot repeat | {"name": "Amit"} |
One Program Using All Collections
# List
students = ["Amit", "Riya", "Karan"]
# Tuple
days = ("Monday", "Tuesday", "Wednesday")
# Set
unique_marks = {80, 90, 80, 75, 90}
# Frozenset
fixed_subjects = frozenset(["Python", "Maths", "English"])
# Dictionary
student_details = {
"name": "Amit",
"age": 15,
"marks": 88
}
print("List:", students)
print("Tuple:", days)
print("Set:", unique_marks)
print("Frozenset:", fixed_subjects)
print("Dictionary:", student_details)
List: ['Amit', 'Riya', 'Karan']
Tuple: ('Monday', 'Tuesday', 'Wednesday')
Set: {80, 90, 75}
Frozenset: frozenset({'Python', 'Maths', 'English'})
Dictionary: {'name': 'Amit', 'age': 15, 'marks': 88}
When Should We Use What?
shopping_list = ["rice", "dal", "milk"]
rgb_color = (255, 0, 0)
roll_numbers = {1, 2, 3, 4}
allowed_subjects = frozenset(["Python", "Java", "SQL"])
student = {
"name": "Amit",
"class": 9,
"city": "Varanasi"
}
Practice Questions
Question 1
Create a list of 5 fruits and print the second fruit.
fruits = ["apple", "banana", "mango", "orange", "grapes"]
print(fruits[1])
Question 2
Create a tuple of 3 numbers and print the last number.
numbers = (10, 20, 30)
print(numbers[2])
Question 3
Create a set with duplicate numbers and print it.
numbers = {1, 2, 2, 3, 3, 4}
print(numbers)
Question 4
Create a frozenset of subjects and check whether "Python" is present.
subjects = frozenset(["Python", "Java", "SQL"])
print("Python" in subjects)
Question 5
Create a dictionary for a student with name, age, and marks.
student = {
"name": "Riya",
"age": 14,
"marks": 92
}
print(student)
print(student["name"])
print(student["marks"])
Mini Project: Student Record System
This mini project combines list, dictionary, set, and tuple.
students = [
{
"name": "Amit",
"age": 15,
"subjects": {"Python", "Maths", "English"},
"marks": (88, 76, 92)
},
{
"name": "Riya",
"age": 14,
"subjects": {"Python", "Science", "English"},
"marks": (91, 85, 89)
}
]
for student in students:
print("Name:", student["name"])
print("Age:", student["age"])
print("Subjects:", student["subjects"])
print("Marks:", student["marks"])
print("Total marks:", sum(student["marks"]))
print()
Name: Amit
Age: 15
Subjects: {'Python', 'Maths', 'English'}
Marks: (88, 76, 92)
Total marks: 256
Name: Riya
Age: 14
Subjects: {'Python', 'Science', 'English'}
Marks: (91, 85, 89)
Total marks: 265
Final Memory Trick
| Collection | Memory Trick |
|---|---|
| List | Changeable line of items |
| Tuple | Fixed line of items |
| Set | Unique items |
| Frozenset | Fixed unique items |
| Dictionary | Key-value data |
Try the Code in Our Python Editor
Use the embedded Python editor below to run and modify the examples.