Python Programming
Module 1_Chapter 3
By
Prathima G
Associate Professor, Dept of CSE(Data Science)
[Link] College,Bangalore
Iteration in Python
• Computers are commonly used to automate repetitive
tasks.
• Iteration refers to the repeated execution of a set of
statements in a program.
• Iteration helps programs perform tasks like:
1. Processing multiple items
2. Repeating calculations
3. Running tasks until a condition is met
• 1. for Loop
• Used to repeat a block of code for a fixed number of
times.
• Often used when working with lists, strings, or
ranges.
• Example: OutPut:
for i in range(5): 0
print(i) 1
2
3
4
while Loop
• Repeats a block of code as long as a condition is true.
• Useful when the number of repetitions is not known in
advance.
• Example:
i=0
OutPut:
while i < 5: 0
print(i) 1
2
i += 1 3
4
Assignment in Python
• In Python, it is legal to assign a value to the same
variable multiple times.
• A new assignment replaces the old value stored in
the variable.
• The variable will then refer to the new value instead
of the previous one. Output:
• Example: 15
7
• airtime_remaining = 15
print(airtime_remaining)
airtime_remaining = 7
print(airtime_remaining)
Assignment vs Equality
• Assignment (=) is used to store a value in a
variable.
• Equality operator (==) is used to compare two
values.
• It is important not to confuse assignment with
equality testing.
• Example:
•a=7 # assignment
a == 7 # equality test
Variables and Reassignment
• Assignment can make two variables have the same
value.
• Later assignments may change one variable without
affecting the other.
• Example: Explanation:
•a=5 Initially: a = 5, b
=5
b = a # After this line, a and b are equal After a = 3:
a = 3 # Now a and b are no longer equal a=3
b=5
• Key Idea:
• Changing one variable does not automatically change
the other.
Updating Variables in Python
• In an assignment statement, the right-hand side expression is
evaluated first.
• After evaluation, the resulting value is assigned to the variable
on the left-hand side.
• This process allows a variable’s value to be updated or changed.
• Example: Explanation:
First: n = 5
•n=5 Next: 3 * 5 + 1 = 16
Final value of n = 16
n=3*n+1
Error When Variable is Not Defined
• A variable must be assigned a value before it is
used.
• If Python encounters a variable with no assigned
value, it produces an error.
• Example:
w=x+1
• Output:
• NameError: name 'x' is not defined
• Explanation:
• Variable x has not been defined before use.
Initializing and Updating Variables
• Before updating a variable, it must be initialized with a
starting value.
• Initialization usually happens with a simple assignment
statement. # Initialize the variable
• Example: runs_scored = 0
# Simulating scoring runs in a game
Output print("Initial runs scored:", runs_scored)
Initial runs scored: 0 # First run scored
Runs scored after first run: runs_scored = runs_scored + 1
1 print("Runs scored after first run:", runs_scored)
Runs scored after second # Second run scored
run: 2 runs_scored = runs_scored + 1
Runs scored after third run: print("Runs scored after second run:", runs_scored)
3 # Third run scored
runs_scored = runs_scored + 1
print("Runs scored after third run:", runs_scored)
The for Loop Revisited
•A for loop processes each item in a list one by one.
•Each item in the list is assigned to the loop variable.
•The body of the loop executes for every item in the list.
• Example:
• for friend in ["Joe", "Zoe", "Zuki", "Thandi",
"Paris"]:
invite = "Hi " + friend + ". Please come to my
party!"
print(invite)
Summing Elements in a List
numbers = [5, 6, 32, 21, 9]
running_total = 0
for number in numbers:
running_total = running_total + number
print(running_total) Final Output:
Sum of the numbers: 73
Iteration Explanation
running_total running_total
Iteration number value Calculation
before after
1 5 0 0+5 5
2 6 5 5+6 11
3 32 11 11 + 32 43
4 21 43 43 + 21 64
5 9 64 64 + 9 73
The while statement
• while <CONDITION>:
• <STATEMENT>
current_sum current_sum
Iteration i value Operation
before after
1 0 0 0+0 0
2 1 0 0+1 1
3 2 1 1+2 3
4 3 3 3+3 6
5 4 6 6+4 10
6 5 10 10 + 5 15
7 6 15 15 + 6 21
Counting digits
Counting 5 or Zero
Tables
Two-dimensional tables
The break statement
The pre-test loop— standard loop
behaviour
for and while loops
The continue statement
Paired Data
year_born = ("Paris Hilton", 1981)
print(year_born)
celebs = [("Brad Pitt", 1963), ("Jack Nicholson", 1937),
("Justin Bieber", 1994)]
print(celebs)
print(celebs)
print(len(celebs))
for name, year in celebs:
if year < 1980:
print(name)
• Nested Loops for Nested Data
students = [
("John", ["CompSci", "Physics"]),
("Vusi", ["Maths", "CompSci", "Stats"]),
("Jess", ["CompSci", "Accounting", "Economics",
"Management"]),
("Sarah", ["InfSys", "Accounting", "Economics",
"CommLaw"]),
("Zuki", ["Sociology", "Economics", "Law", "Stats",
"Music"])]
for name, subjects in students:
print(name, "takes", len(subjects), "courses")