1 of 20

Lecture 4

More Loops & Nested Loops

Summer 2025

Adrian Salguero

1

2 of 20

Announcements

  • Homework 1 is available, due Sunday, July 6th at 11:59pm
  • Coding Practice 1 will be released later today
    • Due Sunday, July 6th at 11:59pm
    • Additional practice on material covered in class
    • Not eligible for resubmission!
  • No class on Friday, July 4th. 🎇🎆

2

3 of 20

Lecture Overview

  • Loops Review
  • Basics of Lists
  • For-each versus index looping
  • Nested Loops
  • Practice Problems!

3

4 of 20

Review: Looping = repeat the same code

  • Looping is a way for us as programmers to control how often a specific code block (i.e. the loop body) runs.
  • Each time a loop body runs, values used may be different (not always)
  • You can loop on different sequences of values
    • Lists → [1, 2, 3, 4]
    • Strings → “CSE160”
    • Range object → represents a sequence of numbers
      • Looks like a list but doesn’t behave like a list

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

5 of 20

Lecture Overview

  • Loops Review
  • Basics of Lists
  • For-each versus index looping
  • Nested Loops
  • Practice Problems!

5

6 of 20

List Basics

  • A List is a collection of values/elements
    • Values can be of different data types (Python does not restrict you, but other programming languages will!)
  • Declaring an empty list
    • list_name = [ ]
  • Declaring a non-empty list
    • list_name = [1, 2.15, “hi”, True]
  • You can get a specific element in a list by using its index
    • Index starts at 0
    • If you reference an index that doesn’t exist → ERROR!

6

list1 = [2, 4, 6, 8, 10]

list1[3]

list1[5]

→ ERROR

len(list1) = 5

7 of 20

Lecture Overview

  • Loops Review
  • Basics of Lists
  • For-each versus index looping
  • Nested Loops
  • Practice Problems!

7

8 of 20

For-each loop versus For-loop using index

  • For-each loop is what we’ve used so far, but there is another way of looping through a sequence when it is a list
  • For-each loop
    • Iterator (“dummy”) variable refers to the element in the sequence/list
  • For-loop with indexing
    • Iterator(“dummy”) variable refers to the index (position) of the element
  • For this example: let’s use list1 = [2, 4, 6, 8, 10]

8

for elem in list1:

print(elem)

for elem in range(len(list1)):

print(list1[elem])

For-each loop

For-loop with indexing

9 of 20

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.

10 of 20

When should I use for-each? Indexing?

  • Loop through each element in a list and obtain the value to use
    • Either one!
  • Loop through each element in a list and update values in specific positions of the list
    • Use index looping since you will need the index to update the list value
    • Simple example: Loop through a list and double each value

Still unsure? Ask yourself these questions

  1. Do I need to know the index/position of these elements?
  2. Does my sequence even allow for indexing? (Some sequences don’t!)
  3. Do I need to modify my sequence in any way?
    1. If it’s a list, most likely you’ll need to loop using the index

10

11 of 20

Lecture Overview

  • Loops Review
  • Basics of Lists
  • For-each versus index looping
  • Nested Loops
  • Practice Problems!

11

12 of 20

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

13 of 20

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

14 of 20

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

15 of 20

Lecture Overview

  • Loops Review
  • Basics of Lists
  • For-each versus index looping
  • Nested Loops
  • Practice Problems!

15

16 of 20

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!

17 of 20

More Loop Practice

Suppose you are given the following two lists:

  • dept = ["CSE", "ECE", "MATH"]
  • course_num = [100, 110, 160]

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

18 of 20

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

19 of 20

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

20 of 20

TODOs

  • Homework 1 due Sunday, July 6th by 11:59pm
  • Coding Practice 1 due Sunday, July 6th by 11:59pm

20