๐Ÿ“˜ Lesson 9: Lists, Tuples, and Sets in Python

๐Ÿ”น 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']

๐Ÿ“Œ Explanation
append() 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

FeatureListTupleSet
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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *