Control Flow — if · for · while · Comprehension Basics
Control Flow — if · for · while · Comprehension Basics
🎯 After reading this lesson
Once you finish this lesson, you will be able to confidently do the following 3 things.
- ▸✅ Indentation rules (PEP 8 — 4 spaces)
- ▸✅ Correct usage of enumerate · zip · range
- ▸✅ List comprehension + dict comprehension
Keep the learning objectives as a checklist and close the lesson once you can answer all of them.
if · elif · else — *Conditional Branching + Indentation Rules*
if / elif / else — The basics of conditional branching
Key rules:
- ▸A colon
:is required at the end of the condition line - ▸The next line must be indented by 4 spaces (block)
- ▸It's
elif, notelse if— Python's own shorthand
Comparison operators: == (equal) / != (not equal) / >= (greater than or equal) / <= (less than or equal) / < / >
Conditional expression (ternary) — one-line if
{value1} if {condition} else {value2} — same as JS's cond ? a : b, but the order is different (value comes first).
truthy / falsy — the hidden rule of if
Python's 7 falsy values: 0, 0.0, '', None, False, [], {}, (). Empty collections are also falsy.
Indentation rules — the #1 cause of SyntaxErrors
Python uses indentation depth instead of { } to delimit blocks. Lines at the same depth belong to the same block.
3 common errors
1. IndentationError: expected an indented block
→ The line after if must be indented. Use pass for an empty block:
2. IndentationError: unexpected indent
3. Mixing tabs and spaces — the invisible trap
→ PEP 8 standard: 4 spaces. Configure your editor to convert Tab → 4 Spaces automatically.
for · while · range · enumerate · zip — *Loops in Full*
Basic for — iterating over an iterable object
There is no index-based for like Java's for(int i=0; i<n; i++). Use range() to generate indices.
range() — numeric sequences
End value is excluded. Same behavior as Java/JS.
enumerate() — index + value at the same time
Use enumerate when you need the index. range(len(arr)) is considered un-Pythonic.
zip() — iterate over multiple sequences simultaneously
Stops automatically based on the shorter sequence. A pattern for processing corresponding elements from two collections.
while and break · continue
for-else — an unusual syntax
The else block runs when the loop finishes normally. Useful for search patterns — a Python-exclusive feature not found in other languages.
List Comprehension — *Python's Identity*
Basic form
4 lines → 1 line. More than half of all Python code generated by AI uses this syntax.
Adding a conditional filter
if at the end — a filter. Equivalent to JS's .filter().map() combined into one line.
Nested comprehensions
Multiple fors — the outer for is the outer loop.
Dict comprehension
Set comprehension
Readability limits
Two or more levels + complex conditions — a regular for loop is easier to read. Comprehensions are powerful only when they're short.
🤖 Try asking AI like this
- ▸"Convert this for loop into a list comprehension"
- ▸"Unwrap this enumerate + zip combination in one shot"
- ▸"Fix this indentation error" (send the whole code along for an instant fix)