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
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.
See the loop? What is the repeated task?
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?
x = 2
y = 5
while (y > 2 and x < y):
x = x + 1
y = y - 1
n=5
while n>0:
print(n)
n=n-1
print('blastoff!')
print(n)
5 4 3 2 1 blastoff! 0
Try implementing it
How do you stop it?
Note the "while True". How else can you achieve the same behavior?
Why might you want to do this?
How might you change the code to run only 100 iterations using "while true"?
Do final 'while' activity
Q-2: What prints if the user’s input is ‘done’?
while True:
line = input('> ')
if line[0] == '#' :
continue
if line == 'done':
break
else:
print(line)
print ('Done!')
Final 'continue' activity
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
What is [5,4,3,2,1]?
5 in [5,4,3,2,1]
True
Make a cheat sheet for all the new tricks you need to memorize in order to use them (no working examples, just useful facts)
Python has a built-in function for this
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
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
https://runestone.academy/ns/books/published/py4e-int/iterations/Exercises.html