Skip to main content

Loops

For sure — let’s make your loop set fully interview-ready by adding the one remaining classic: the while loop!


✅ Your 4 Essential Python Loops for Coding Interviews

Here’s the final list you should know, with when to use each and examples you can write from memory.


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

for i in range(5):
    print(i)

for i in range(len(colors)):
    print(i, colors[i])

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?