Skip to main content

Loops

loops-to-use.png

🟒 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?