Python control flow uses indentation (typically four spaces) instead of braces—unlike JavaScript or Java. Colons start blocks after if, elif, else, for, while, and def.
if / elif / else
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
for and while
for item in ["a", "b", "c"]:
print(item)
n = 3
while n > 0:
print(n)
n -= 1
for iterates any iterable—lists, strings, dict keys, ranges. Use range(n) for counted loops.
Important interview questions and answers
- Q: Truthiness in Python?
A: Empty collections,0,None, andFalseare falsy; most other objects are truthy. - Q: break vs continue?
A:breakexits the loop;continueskips to the next iteration.
Self-check
- What syntax starts a block after if?
- How do you loop 0 through 4 inclusive?
Tip: Python has no switch until 3.10+ match—use if/elif chains or dict dispatch for simple cases.
Interview prep
- Indentation rules?
Consistent spaces (usually 4) after
:define blocks—mixing tabs and spaces causes errors.- Truthy values?
Empty collections, zero numbers,
None, andFalseare falsy; most other objects are truthy.