๐
STUDENT NOTES ยท DAY 5 OF 24
The Repeat Machine ๐
Why type something 100 times when Bittu can repeat it for you? Today: for loops, while loops, and the mighty break.
1The for loop โ repeat a fixed number of times
๐
Py: A for loop is like roll-call: go through the numbers one by one, do the same thing for each.
๐ฆ
Professor Hoot: range(1, 6) gives 1,2,3,4,5 โ the end number is never included. It's shy.
Range visualizer โ slide the end number!
range(5)
0 1 2 3 4
2Loops do real work
Remember writing multiplication tables in a notebook? Two lines of Python:
๐ค
Bittu: Table of 7 in 0.0001 seconds. Ask me for the table of 40404 โ I don't care.
3The while loop โ repeat UNTIL something changes
A while loop doesn't count โ it keeps going as long as its test stays True:
๐
Bugly: Heheheโฆ forget the count = count - 1 line and the loop runs FOREVER. My favourite trap! (Press the stop โ button in Colab if it happens.)
4break โ the emergency exit
๐
Py: while True loops forever on purpose โ until break smashes the exit. Every game loop ever is built like this.
5Quick quiz! ๐ง
What is the LAST number printed by for i in range(1, 6): print(i)?
๐ก range(1, 6) stops BEFORE 6. Prints 1,2,3,4,5.
A while loop's test never becomes False. What happens?
๐ก Nothing stops it! Something inside the loop must change, or you meet the infinite loop.
6Your mission ๐
Mission 1: Print your name 10 times with a for loop.
Mission 2: Print the table of any number the user types (input + int + loop).
Mission 3: Make a countdown from 10 to 1 that ends with "๐ LIFTOFF!".
Day 5 done โ you command the Repeat Machine! ๐
Next: loops inside loops, skipping with continue, and star pyramids.
Go to Day 6: Loop Tricks โ