๐
STUDENT NOTES ยท DAY 6 OF 24
Loop Tricks & Patterns ๐
Loops inside loops, skipping the boring bits, and collecting totals โ the tricks that turn loops into superpowers.
1A loop inside a loop
๐
Py: Think of a school timetable: for every DAY, go through every PERIOD. That's a loop inside a loop!
๐ฆ
Professor Hoot: The outer loop moves once; the inner loop runs ALL its rounds each time. 2 outer ร 3 inner = 6 lines.
2continue โ skip this one, keep going
๐ค
Bittu: continue = skip the rest of THIS round and move to the next. break = leave the loop completely. Different exits!
3The collector pattern
Start with an empty total, add to it every round โ how programs sum anything:
4Star patterns! โญ
Pyramid builder โ slide the height!
height = 4
โ
โ โ
โ โ โ
โ โ โ โ
โ โ
โ โ โ
โ โ โ โ
5Quick quiz! ๐ง
How many lines print?
for i in range(3):
for j in range(2): print(i, j)
for i in range(3):
for j in range(2): print(i, j)
๐ก Outer runs 3 times, inner runs 2 per outer round: 3 ร 2 = 6 lines.
Inside a loop, continue does what?
๐ก It abandons the current round and jumps straight to the next one.
6Your mission ๐
Mission 1: Print numbers 1โ20, but SKIP all numbers divisible by 3 (hint: % and continue).
Mission 2: Add up all numbers from 1 to 100 with the collector pattern. (Check: it's 5050!)
Mission 3: Build an UPSIDE-DOWN star pyramid. ๐
Day 6 done โ pattern master! ๐
Next: lists โ one variable that holds a whole class of students.
Go to Day 7: Lists โ