๐ฏ Lesson Objective
To understand how to work with Pythonโs most common collection data types โ Lists, Tuples, and Sets, and how to manipulate and use them effectively.
๐งฉ 1. Lists
A list is a mutable (changeable) collection of items, written with square brackets [ ].
โ Example:
fruits = ["apple", "banana", "cherry"]
print(fruits)
Output:
['apple', 'banana', 'cherry']
โ๏ธ List Operations
| Operation | Example | Output |
|---|---|---|
| Access element | fruits[0] | apple |
| Negative index | fruits[-1] | cherry |
| Slice | fruits[0:2] | ['apple', 'banana'] |
| Length | len(fruits) | 3 |
| Add item | fruits.append('orange') | ['apple', 'banana', 'cherry', 'orange'] |
| Insert item | fruits.insert(1, 'mango') | ['apple', 'mango', 'banana', 'cherry'] |
| Remove item | fruits.remove('banana') | ['apple', 'cherry'] |
| Pop item | fruits.pop() | removes last element |
| Reverse | fruits.reverse() | reversed list |
| Sort | fruits.sort() | alphabetically sorted |
๐ Looping Through a List
numbers = [10, 20, 30, 40]
for num in numbers:
print(num)
Output:
10
20
30
40
๐งฎ List Comprehension
A short way to create lists.
squares = [x**2 for x in range(5)]
print(squares)
Output:
[0, 1, 4, 9, 16]
๐งฑ 2. Tuples
A tuple is an immutable (unchangeable) sequence, written with parentheses ( ).
โ Example:
colors = ("red", "green", "blue")
print(colors)
Output:
('red', 'green', 'blue')
โ๏ธ Tuple Operations
| Operation | Example | Output |
|---|---|---|
| Access | colors[1] | green |
| Length | len(colors) | 3 |
| Count | colors.count('red') | 1 |
| Index | colors.index('blue') | 2 |
Note: You cannot modify a tuple (no append/remove/sort).
๐ Looping Through a Tuple
for color in colors:
print(color)
Output:
red
green
blue
๐ Tuple Unpacking
person = ("Sameer", 25, "Warangal")
name, age, city = person
print(name)
print(age)
print(city)
Output:
Sameer
25
Warangal
๐ท 3. Sets
A set is an unordered, unique collection written with curly braces { }.
โ Example:
fruits = {"apple", "banana", "cherry", "apple"}
print(fruits)
Output:
{'apple', 'banana', 'cherry'} # Duplicates removed
โ๏ธ Set Operations
| Operation | Example | Output |
|---|---|---|
| Add item | fruits.add('orange') | adds one item |
| Update | fruits.update(['mango','grape']) | adds multiple items |
| Remove | fruits.remove('banana') | removes banana |
| Discard | fruits.discard('kiwi') | no error if missing |
| Clear | fruits.clear() | empty set |
๐ข Set Mathematical Operations
Sets are useful for union, intersection, and difference.
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print(a.union(b)) # {1, 2, 3, 4, 5, 6}
print(a.intersection(b)) # {3, 4}
print(a.difference(b)) # {1, 2}
๐ Looping Through a Set
for item in {"cat", "dog", "bird"}:
print(item)
๐ง 4. Key Differences
| Feature | List | Tuple | Set |
|---|---|---|---|
| Syntax | [ ] | ( ) | { } |
| Mutable | โ Yes | โ No | โ Yes |
| Ordered | โ Yes | โ Yes | โ No |
| Allows Duplicates | โ Yes | โ Yes | โ No |
| Indexed | โ Yes | โ Yes | โ No |
๐ 5. Real-Life Example โ Student Grades
students = ["Ali", "Sara", "John"]
grades = (85, 90, 78)
subjects = {"Math", "Science", "English"}
print("Students:", students)
print("Grades:", grades)
print("Subjects:", subjects)
Output:
Students: ['Ali', 'Sara', 'John']
Grades: (85, 90, 78)
Subjects: {'Math', 'Science', 'English'}

Leave a Reply