While Loops, For Loops & Range
while repeats a block while a condition is True. Watch for infinite loops — update the condition inside.
Python While Loops
while repeats a block while a condition is True. Watch for infinite loops — update the condition inside.
break exits the loop; continue skips to the next iteration.
Real-life example: while is like stirring soup until it boils — keep going until the condition changes.
count = 3
while count > 0:
print(count)
count -= 1
print("Go!")Python For Loops
for item in iterable loops over lists, strings, dicts, and more. Prefer for when you know what you are iterating.
Use enumerate() when you need both index and value.
Real-life example: for is like checking each student in a roll call — one name at a time until the list ends.
topics = ["syntax", "loops", "functions"]
for i, topic in enumerate(topics, start=1):
print(i, topic.title())Python Range
range(stop), range(start, stop), range(start, stop, step) generate number sequences without storing a full list.
Common pattern: for i in range(5):
Real-life example: range is a numbered ticket dispenser — it hands you 0, 1, 2… without printing all tickets at once.
print(list(range(5)))
print(list(range(2, 10, 2)))