← Lesson
BitWithBite
Python · Quick Reference

Lesson 7 — Loops Cheat Sheet

Python
In one line: Loops let your program repeat a block of code without you having to write it multiple times. Python has exactly two loop types — each suited to different situations.

Key Ideas

1Two Types of Loops. Loops let your program repeat a block of code without you having to write it multiple times. Python has exactly two loop types — each suited to different situations.
2The for Loop. A for loop steps through each item in a sequence one by one, assigning it to a variable you name. When the sequence is exhausted, the loop ends automatically.
3The while Loop. A while loop repeats its body as long as its condition remains True. The condition is checked before each iteration — if it's already False at the start, the body neve...
4Loop Control — break, continue, pass. Three special keywords give you fine-grained control over loop execution.
5enumerate() & zip(). Two built-in functions that make for loops dramatically more powerful and Pythonic — eliminating the need for manual counter variables.

Code Examples

# Looping over a list fruits = ["apple", "banana", "mango"] for fruit in fruits: print(f"I like {fruit}") # I like apple # I like banana # I like mango # Looping over a string (character by character) for char in "Python": print(char, end="-") ...
# Print 1 to 5 for i in range(1, 6): print(i, end=" ") # 1 2 3 4 5 print() # Countdown from 5 for i in range(5, 0, -1): print(i, end=" ") # 5 4 3 2 1 print("🚀") # Sum of 1 to 100 total = 0 for n in range(1, 101): total += n print(total) ...
# Basic while count = 1 while count 5: print(count, end=" ") count += 1 # ← MUST update or infinite loop! # 1 2 3 4 5 # Keep asking until valid input — very common pattern age = -1 while age 0 or age > 120: age = int(input("Enter val...