for Loop in Python — Python Technical Interview Questions & Answers
for Loop in Python
for Loop in Python
- for walks each item in an iterable, one by one. Shape: for name in names: print(name). You can loop a list, a string, a tuple, dict keys, or range. range(5) is 0..4. range(1, 6) is 1..5. The stop is exclusive — that off-by-one mistake prints 1..6 when you wanted 1..5.
- enumerate(names) gives index and value together: for i, n in enumerate(names):. Dry-run: for i in range(1, 4): print(i) → 1 then 2 then 3. Say the stop is exclusive before you write the code.
- for Loop in Python — output: 1 2 3 4 5 each on a new line. range(1, 6) stops before 6.
names = [Asha, Ravi, Neha]
│
▼
for x in names:
Asha → Ravi → NehaTechnical Interview Tip: range stop is exclusive — say it clearly.
Code Examples for for Loop in Python
Example
# for loop
for i in range(1, 6):
print(i)
for Loop in Python — output: 1 2 3 4 5 each on a new line. range(1, 6) stops before 6.
How to Explain for Loop in Python to an Interviewer
Explain for Loop as if you are teaching a junior — definition, then one tiny script.
for x in iterable. range stop is exclusive.
What does the example print, and why?
for Loop in Python — output: 1 2 3 4 5 each on a new line
What mistake do freshers make with for Loop?
enumerate for index.