Loops in Python — Python Technical Interview Questions & Answers
Loops in Python
Loops in Python
- A loop repeats a block until you stop it. for walks each item in a list, string, or range. while repeats while a condition stays true. Pick for when you know the items. Pick while when you wait until something becomes false — like “until the user types quit”.
- break leaves the loop. continue skips the rest of this round. pass does nothing. while True without a break or an update never ends — the terminal just sits there. That infinite-loop trap is worth one sentence in the viva.
- Loops in Python — output: for 1 2 3 then while 3 2 1.
Technical Interview Tip: Define loop, then for vs while in one line each.
Code Examples for Loops in Python
Example
# Loops
for i in range(1, 4):
print("for", i)
n = 3
while n > 0:
print("while", n)
n -= 1
Loops in Python — output: for 1 2 3 then while 3 2 1.
How to Explain Loops in Python to an Interviewer
Explain Loops as if you are teaching a junior — definition, then one tiny script.
Repeat a block. for = items. while = condition.
What does the example print, and why?
Loops in Python — output: for 1 2 3 then while 3 2 1
What mistake do freshers make with Loops?
Infinite while True.