Lecture 4
More Loops & Nested Loops
Summer 2025
Adrian Salguero
1
Announcements
2
Lecture Overview
3
Review: Looping = repeat the same code
4
for fahr in [30, 40, 50, 60, 70, 80]:
cent = (fahr - 32) / 9.0 * 5
print(fahr, cent)
print("All done")
Loop body�is indented
Lecture Overview
5
List Basics
6
list1 = [2, 4, 6, 8, 10]
list1[3]
list1[5]
→ ERROR
len(list1) = 5
Lecture Overview
7
For-each loop versus For-loop using index
8
for elem in list1:
print(elem)
for elem in range(len(list1)):
print(list1[elem])
For-each loop
For-loop with indexing
Looping over Strings using Indexing
course = "CSE161"
for i in range(0, len(course)):
print(course[i])
9
Can we update this string using indexing?
NO!
We will discuss why in the future.
When should I use for-each? Indexing?
Still unsure? Ask yourself these questions
10
Lecture Overview
11
Nested Loops!
12
for i in range(3):
for j in range(2):
print("i is", i)
print("j is", j)
print("*************************")
print("All done!")
i is 0
j is 0
i is 0
j is 1
*************************
i is 1
j is 0
i is 1
j is 1
*************************
i is 2
j is 0
i is 2
j is 1
*************************
All done!
Output
Another Nested Loop
for i in range(2):
print("Outer", i)
for j in range(2,4):
print(" Inner", j)
print(" Sum", i + j)
print("Outer", i)
13
Output
Outer 0
Inner 2
Sum 2
Inner 3
Sum 3
Outer 0
Outer 1
Inner 2
Sum 3
Inner 3
Sum 4
Outer 1
Another Nested Loop
for i in range(2):
print("Outer", i)
for j in range(2,4):
print(" Inner", j)
print(" Sum", i + j)
print("Outer", i)
14
loop body:�3 statements
“nested”�loop body:�2 statements
i does not update until nested loop completely finishes running
Lecture Overview
15
Test your Knowledge
for i in [0,1]:
print(i)
print(i)
i = 5
for i in range(10, 0):
print(i)
16
for i in range(0, 2):
print("Outer", i)
for i in range(2, 4):
print(" Inner", i)
print("Outer", i)
Be careful with which variables you use in your loops → don’t risk overwriting the iterator variable with another loop!
More Loop Practice
Suppose you are given the following two lists:
Write code that prints out all possible combinations of department names with course numbers. For example, your code should output exactly:
CSE 100, CSE 110, CSE 160, ECE 100, …, MATH 160
Note: Yes…including the spaces between the department and course number. Commas are optional, you can print each on a new line.
17
More Loop Practice
You are asked to review some code that prints all the numbers starting from 1 through to 50. The code is given below:
for tens_digit in [0, 1, 2, 3, 4]:
for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
print(tens_digit * 10 + ones_digit)
However, when you run this code the output is incorrect! How would you change it so the output is correct without rewriting the for loops from scratch?
18
Some Fixes for the Previous Problem
for tens_digit in [0, 1, 2, 3, 4]:
for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
print(tens_digit * 10 + ones_digit + 1)
for tens_digit in [0, 1, 2, 3, 4]:
for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
print(tens_digit * 10 + ones_digit)
for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
print(ones_digit)
for tens_digit in [1, 2, 3, 4]:
for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
print(tens_digit * 10 + ones_digit)
print(50)
19
TODOs
20