Loops
π’ Standard
Use when you want to loop directly over the items (values only).
Input | Example | Output |
---|---|---|
["apple", "banana"] |
for fruit in ["apple", "banana"]: print(fruit) |
apple banana |
"hi" |
for char in "hi": print(char) |
h i |
{ "a": 1, "b": 2 } |
for key in {"a": 1, "b": 2}: print(key) |
a b |
dict.items() |
for k, v in {"a": 1}.items(): print(k, v) |
a 1 |
π· RANGE LOOPS
Use when you need to count, index, or control the steps of iteration.
Input | Example | Output |
---|---|---|
range(3) |
for i in range(3): print(i) |
0 1 2 |
range(2, 5) |
for i in range(2, 5): print(i) |
2 3 4 |
range(0, 6, 2) |
for i in range(0, 6, 2): print(i) |
0 2 4 |
range(3, 0, -1) |
for i in range(3, 0, -1): print(i) |
3 2 1 |
range(len(list)) |
for i in range(len(fruits)): print(fruits[i]) |
values in fruits |
π£ ENUMERATE LOOPS
Use when you want both index and value from a list or iterable.
Input | Example | Output |
---|---|---|
["x", "y"] |
for i, v in enumerate(["x", "y"]): print(i, v) |
0 x 1 y |
["a", "b", "c"] |
for i, letter in enumerate(["a", "b", "c"]): ... |
0 a 1 b 2 c |
π§± NESTED LOOPSΒ
Loop Type | Input | Example | Output |
---|---|---|---|
π· Range | range(2), range(2) |
for i in range(2): for j in range(2): print(i, j) |
0 0 , 0 1 , 1 0 , 1 1 |
π· Range | 2D array (matrix ) by index |
for i in range(len(matrix)): for j in range(len(matrix[0])): |
Access each matrix[i][j] |
π· Range | All pairs in list | for i in range(len(nums)): for j in range(i+1, len(nums)): |
Pairs (i, j) |
π£ Enumerate | List of lists (2D grid) | for i, row in enumerate(grid): for j, val in enumerate(row): |
(i, j, val) per cell |
π’ Normal | List of lists (values only) | for row in matrix: for val in row: print(val) |
Each cell value |
π’ Normal | Two arrays (cross product) | for a in ["a", "b"]: for n in ["1", "2"]: print(a, n) |
a 1 , a 2 , b 1 , b 2 |
1. β
Standard for
loop
Use when you want to iterate over items in a list, set, or string.
colors = ['red', 'green', 'blue']
for color in colors:
print(color)
2. β
enumerate()
loop
Use when you need both the index and the value.
for i, color in enumerate(colors):
print(i, color)
3. β
range()
loop
Use when you want to:
-
Loop a specific number of times
-
Access list items by index
-
Do nested loops
4. β
while
loop
Use when:
-
You donβt know how many times youβll loop
-
Youβre waiting for a condition to change
x = 0
while x < 5:
print(x)
x += 1
π Warning: Always make sure the condition will eventually become False
or you'll get an infinite loop!
π§ When to Use Which?
Loop Type | When to Use |
---|---|
for item in list |
Clean, readable, item-based loops |
enumerate() |
Index + value |
range() |
Loop by number or index, brute-force logic |
while |
Loop while a condition is true (uncertain end) |
Optional Daily Practice Prompts
Want to write one of each daily?
-
β
for item in list
: print all characters in "hello" -
β
enumerate()
: print index and item of a fruit list -
β
range()
: print numbers 1 to 10 -
β
while
: count down from 5 to 0
Want me to add this to your daily fundamentals checklist or export it as a mini Anki flashcard set or markdown file you can keep open?
No Comments