๐น 1. Lists in Python
What is a List?
A list is a collection of multiple values stored in square brackets [ ].
โ Ordered
โ Changeable (mutable)
โ Allows duplicate values
Example 1: Creating a List
students = ["Sameer", "Ayesha", "Saddam", "Chandini"]print(students)
Output
['Sameer', 'Ayesha', 'Saddam', 'Chandini']
๐ Explanation
- List stores multiple values in one variable.
Example 2: Accessing List Elements
students = ["Sameer", "Ayesha", "Saddam"]print(students[0])
print(students[2])
Output
Sameer
Saddam
๐ Explanation
List indexing starts from 0.
Example 3: Changing List Values
students = ["Sameer", "Ayesha", "Saddam"]students[1] = "Gousya"print(students)
Output
['Sameer', 'Gousya', 'Saddam']
๐ Explanation
Lists are mutable, so values can be changed.
Example 4: Adding Items
students = ["Sameer", "Ayesha"]students.append("Chandini")print(students)
Output
['Sameer', 'Ayesha', 'Chandini']
๐ Explanationappend() adds an item to the end of the list.
Example 5: Removing Items
students = ["Sameer", "Ayesha", "Saddam"]students.remove("Ayesha")print(students)
Output
['Sameer', 'Saddam']
Example 6: List Length
students = ["Sameer", "Ayesha", "Saddam"]print(len(students))
Output
3
Example 7: Loop Through List
students = ["Sameer", "Ayesha", "Gousya"]for student in students:
print(student)
Output
Sameer
Ayesha
Gousya
๐น 2. Tuples in Python
What is a Tuple?
A tuple is a collection stored in parentheses ( ).
โ Ordered
โ Not changeable (immutable)
โ Allows duplicates
Example 8: Creating a Tuple
students = ("Sameer", "Ayesha", "Saddam")print(students)
Output
('Sameer', 'Ayesha', 'Saddam')
Example 9: Access Tuple Elements
students = ("Sameer", "Ayesha", "Saddam")print(students[1])
Output
Ayesha
Example 10: Tuple Length
students = ("Sameer", "Ayesha", "Saddam")print(len(students))
Output
3
Example 11: Loop Through Tuple
students = ("Sameer", "Ayesha", "Gousya")for student in students:
print(student)
Output
Sameer
Ayesha
Gousya
Example 12: Tuple Cannot Be Changed
students = ("Sameer", "Ayesha", "Saddam")students[1] = "Gousya"
Output
TypeError: 'tuple' object does not support item assignment
๐ Explanation
Tuples are immutable.
๐น 3. Sets in Python
What is a Set?
A set is a collection stored in curly brackets { }.
โ Unordered
โ No duplicate values
โ Mutable
Example 13: Creating a Set
students = {"Sameer", "Ayesha", "Saddam"}print(students)
Output
{'Sameer', 'Ayesha', 'Saddam'}
๐ Order may change because sets are unordered.
Example 14: Duplicate Values Removed
students = {"Sameer", "Ayesha", "Sameer"}print(students)
Output
{'Sameer', 'Ayesha'}
Example 15: Adding Item to Set
students = {"Sameer", "Ayesha"}students.add("Chandini")print(students)
Output
{'Sameer', 'Ayesha', 'Chandini'}
Example 16: Removing Item
students = {"Sameer", "Ayesha", "Saddam"}students.remove("Ayesha")print(students)
Output
{'Sameer', 'Saddam'}
๐น 4. Difference Between List, Tuple, and Set
| Feature | List | Tuple | Set |
|---|---|---|---|
| Brackets | [ ] | ( ) | { } |
| Ordered | โ | โ | โ |
| Changeable | โ | โ | โ |
| Duplicate values | โ | โ | โ |
๐น 5. Real-Life Example
Student Registration
students = ["Sameer", "Ayesha", "Gousya"]students.append("Chandini")for student in students:
print("Registered:", student)
Output
Registered: Sameer
Registered: Ayesha
Registered: Gousya
Registered: Chandini

Leave a Reply