Loops and Iteration

Spring 2026

https://www.py4e.com/lessons/loops

https://runestone.academy/ns/books/published/py4e-int/iterations/toctree.html

https://www.keithdillon.com/index.php/python-for-data-science-spring-2026/

GitHub Classroom: https://classroom.github.com/classrooms/250035910-keithops-bds754-s26

Google CoLab: https://colab.research.google.com/notebooks/welcome.ipynb

slide

Motivation¶

  • send personalized emails to everyone in mailing list
  • Compute $\sum_{k=0}^{100000} \dfrac{1}{k}$
  • Fiz buzz interview question https://leetcode.com/problems/fizz-buzz/description/

    Given an integer n, return a string array answer (1-indexed) where:

      answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
      answer[i] == "Fizz" if i is divisible by 3.
      answer[i] == "Buzz" if i is divisible by 5.
      answer[i] == i (as a string) if no conditions are true.
  • Game that creates an animation each time the player hits a trigger button
slide
slide

See the loop? What is the repeated task?

  • initialized a variable
  • while statement
  • update counter (n)
  • automatically return to beginning of loop

New trick #1: updating variables:¶

x=x+1

Try this out

New trick #2: while statement:¶

A combination of a condition and an indication to python that you're starting a loop.

Try the py4e-int activity

Q-4: Consider the code block below. What are the values of x and y when this while loop finishes executing?

In [1]:
x = 2
y = 5

while (y > 2 and x < y):
    x = x + 1
    y = y - 1
slide
In [2]:
n=5
while n>0:
    print(n)
    n=n-1
print('blastoff!')
print(n)
5
4
3
2
1
blastoff!
0
slide

Try implementing it

How do you stop it?

qualcomm_brick_loop
slide
slide

Note the "while True". How else can you achieve the same behavior?

Why might you want to do this?

slide
slide

How might you change the code to run only 100 iterations using "while true"?

Do final 'while' activity

slide
slide
slide

Activity 6.4.2¶

Q-2: What prints if the user’s input is ‘done’?

In [ ]:
while True:
    line = input('> ')
    if line[0] == '#' :
        continue
    if line == 'done':
        break
    else:
        print(line)
print ('Done!')

Final 'continue' activity

slide
slide
slide

What kind of loops are these?¶

  • send personalized emails to everyone in mailing list
  • Compute $\sum_{k=0}^{100000} \dfrac{1}{k}$
  • Fiz buzz interview question https://leetcode.com/problems/fizz-buzz/description/

    Given an integer n, return a string array answer (1-indexed) where:

      answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
      answer[i] == "Fizz" if i is divisible by 3.
      answer[i] == "Buzz" if i is divisible by 5.
      answer[i] == i (as a string) if no conditions are true.
  • Game that creates an animation each time the player hits a trigger button

slide

What is [5,4,3,2,1]?

slide
slide
slide
In [6]:
5 in [5,4,3,2,1]
Out[6]:
True
slide
slide

Exercise: Do the same thing with a while loop?¶

In [ ]:
 

End of 6.5 exercise¶

slide
slide

Recap¶

Make a cheat sheet for all the new tricks you need to memorize in order to use them (no working examples, just useful facts)

slide
slide
slide

Python has a built-in function for this

slide
slide
slide
slide
slide
slide
slide
slide

"print debugging"¶

Gather info When your code doesn't work. You can't always use a debugger.

Edit "min" loop to tell you what is going on internally and help you update it

In [7]:
largest = None
print('Before:', largest)
for itervar in [3, 41, 12, 9, 74, 15]:
    if largest is None or itervar > largest :
        largest = itervar
    print('largest so far', largest)
print('Largest:', largest)
Before: None
largest so far 3
largest so far 41
largest so far 41
largest so far 41
largest so far 74
largest so far 74
Largest: 74
slide
slide
slide
slide
slide

Multiple choice questions¶

https://runestone.academy/ns/books/published/py4e-int/iterations/Exercises.html

slide