Python Programming Lecture 3
Python Programming: Lecture 3
Lists, Tuples, Dictionaries, and Sets
Introduction: Why Do We Need Data
Structures?
In Lecture 1, we learned how to store values in variables:
x = 10
name = " Ali "
But what if we want to store 100 student marks, a list of names, or multiple sensor
readings?
Key Idea
A data structure allows us to store and organize multiple values in one variable.
Python provides four very important built-in data structures:
• List (list)
• Tuple (tuple)
• Dictionary (dict)
• Set (set)
Lists in Python (list)
A list is an ordered collection of items. Lists are:
• Ordered (items have positions)
• Mutable (can be changed)
• Can store different data types
Creating a List
numbers = [10 , 20 , 30 , 40]
names = [ " Ali " , " Ahmed " , " Sara " ]
mixed = [10 , " Python " , 3.14 , True ]
1
Python Programming Lecture 3
Accessing Elements (Indexing)
List indices start from 0.
names = [ " Ali " , " Ahmed " , " Sara " ]
print ( names [0]) # Ali
print ( names [2]) # Sara
Negative Indexing
names = [ " Ali " , " Ahmed " , " Sara " ]
print ( names [ -1]) # Sara
print ( names [ -2]) # Ahmed
Modifying List Elements
numbers = [10 , 20 , 30]
numbers [1] = 99
print ( numbers ) # [10 , 99 , 30]
List Length
numbers = [10 , 20 , 30 , 40]
print ( len ( numbers ) ) # 4
Adding Elements
append()
Adds one element at the end.
numbers = [1 , 2 , 3]
numbers . append (4)
print ( numbers )
insert()
Adds an element at a specific index.
numbers = [1 , 2 , 3]
numbers . insert (1 , 100)
print ( numbers )
extend()
Adds multiple elements from another list.
a = [1 , 2]
b = [3 , 4 , 5]
a . extend ( b )
print ( a )
2
Python Programming Lecture 3
Removing Elements
remove()
Removes by value.
nums = [10 , 20 , 30 , 20]
nums . remove (20)
print ( nums )
pop()
Removes by index.
nums = [10 , 20 , 30]
nums . pop (1)
print ( nums )
clear()
Removes everything.
nums = [1 , 2 , 3]
nums . clear ()
print ( nums )
Slicing Lists
Slicing extracts a portion of the list.
nums = [10 , 20 , 30 , 40 , 50]
print ( nums [1:4]) # [20 , 30 , 40]
print ( nums [:3]) # [10 , 20 , 30]
print ( nums [2:]) # [30 , 40 , 50]
Looping Through Lists
names = [ " Ali " , " Ahmed " , " Sara " ]
for n in names :
print ( " Hello " , n )
Common List Functions
nums = [5 , 2 , 9 , 1]
print ( min ( nums ) )
print ( max ( nums ) )
print ( sum ( nums ) )
nums . sort ()
print ( nums )
3
Python Programming Lecture 3
Important Note
sort() modifies the list permanently. If you want a temporary sorted list, use
sorted(nums).
Tuples in Python (tuple)
A tuple is similar to a list, but it is:
• Ordered
• Immutable (cannot be changed)
Creating a Tuple
coordinates = (10 , 20)
colors = ( " red " , " green " , " blue " )
Accessing Tuple Elements
coordinates = (10 , 20)
print ( coordinates [0])
print ( coordinates [1])
Why Use Tuples?
Tuples are used when:
• Data should not change
• We want safer code
• We store fixed values like coordinates, RGB values, etc.
Tuple is Immutable
This will cause an error:
coordinates = (10 , 20)
coordinates [0] = 99
Tuple Unpacking
point = (5 , 8)
x , y = point
print ( x )
print ( y )
4
Python Programming Lecture 3
Dictionaries in Python (dict)
A dictionary stores values in key-value pairs.
Dictionary Properties
A dictionary is:
• Unordered (in older Python versions)
• Stores values using unique keys
• Very fast for searching
Creating a Dictionary
student = {
" name " : " Ali " ,
" age " : 20 ,
" grade " : " A "
}
Accessing Dictionary Values
print ( student [ " name " ])
print ( student [ " age " ])
Modifying a Dictionary
student [ " age " ] = 21
student [ " city " ] = " Lahore "
print ( student )
Dictionary Functions
print ( student . keys () )
print ( student . values () )
print ( student . items () )
Checking if a Key Exists
if " grade " in student :
print ( " Grade exists ! " )
Looping Through a Dictionary
for key in student :
print ( key , " : " , student [ key ])
5
Python Programming Lecture 3
Looping Through Key-Value Pairs
for key , value in student . items () :
print ( key , " = " , value )
Sets in Python (set)
A set is a collection of unique values.
Creating a Set
numbers = {1 , 2 , 3 , 4}
Sets Remove Duplicates Automatically
nums = {1 , 2 , 2 , 3 , 3 , 3 , 4}
print ( nums )
Adding and Removing Elements in Sets
s = {1 , 2 , 3}
s . add (10)
s . remove (2)
print ( s )
Set Operations
A = {1 , 2 , 3 , 4}
B = {3 , 4 , 5 , 6}
print ( A . union ( B ) )
print ( A . intersection ( B ) )
print ( A . difference ( B ) )
6
Python Programming Lecture 3
Comparison Table: List vs Tuple vs
Dictionary vs Set
Type Ordered? Mutable? Duplicates?
List Yes Yes Yes
Tuple Yes No Yes
Dictionary Yes (Python Yes Keys No
3.7+)
Set No Yes No
Mini Examples (Important Practice)
Example 1: Average of List
nums = [10 , 20 , 30 , 40]
average = sum ( nums ) / len ( nums )
print ( " Average = " , average )
Example 2: Student Record Using Dictionary
student = {
" name " : " Sara " ,
" marks " : [80 , 90 , 85]
}
avg = sum ( student [ " marks " ]) / len ( student [ " marks " ])
print ( " Student : " , student [ " name " ])
print ( " Average Marks : " , avg )
Example 3: Removing Duplicates Using Set
names = [ " Ali " , " Sara " , " Ali " , " Ahmed " , " Sara " ]
unique_names = set ( names )
print ( unique_names )
7
Python Programming Lecture 3
Practice Tasks (Homework)
Task 1: List Practice
Create a list of 5 numbers.
• Print the list
• Print the maximum number
• Print the minimum number
• Print the sum of all numbers
Task 2: List and Loop
Create a list of names. Loop through the list and print "Hello <name>" for each person.
Task 3: Tuple Practice
Create a tuple containing your city and your country. Print each value separately.
Task 4: Dictionary Practice
Create a dictionary for a student containing:
• name
• age
• marks (list of marks)
Then compute and print the average marks.
Task 5: Set Practice
Create a list of 10 numbers (with duplicates). Convert it to a set and print the set.
Challenge Task
Write a program that:
• Takes 5 names from the user
• Stores them in a list
• Removes duplicates using a set
• Prints the final unique names
Common Beginner Mistakes
• Confusing list [] with tuple ()
8
Python Programming Lecture 3
• Trying to modify a tuple (not possible)
• Forgetting dictionary keys must be unique
• Expecting sets to maintain order
Lecture Summary
• Lists store multiple values and are mutable
• Tuples are like lists but immutable
• Dictionaries store key-value pairs
• Sets store unique values and remove duplicates
• Data structures are essential for real programming
Next Lecture: Functions in Python