Lesson 9: Lists, Tuples, and Sets

๐ŸŽฏ 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

OperationExampleOutput
Access elementfruits[0]apple
Negative indexfruits[-1]cherry
Slicefruits[0:2]['apple', 'banana']
Lengthlen(fruits)3
Add itemfruits.append('orange')['apple', 'banana', 'cherry', 'orange']
Insert itemfruits.insert(1, 'mango')['apple', 'mango', 'banana', 'cherry']
Remove itemfruits.remove('banana')['apple', 'cherry']
Pop itemfruits.pop()removes last element
Reversefruits.reverse()reversed list
Sortfruits.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

OperationExampleOutput
Accesscolors[1]green
Lengthlen(colors)3
Countcolors.count('red')1
Indexcolors.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

OperationExampleOutput
Add itemfruits.add('orange')adds one item
Updatefruits.update(['mango','grape'])adds multiple items
Removefruits.remove('banana')removes banana
Discardfruits.discard('kiwi')no error if missing
Clearfruits.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

FeatureListTupleSet
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'}


Comments

Leave a Reply

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