Beginner·7 min·basics · control flow
If / Else & Loops
You’ll be able to
- Write
if,elif, andelsebranches to make decisions - Iterate over ranges with
forandrange() - Combine conditions using
and,or,not - Understand Python's indentation rules for code blocks
Why this matters
Clean branching separates readable code from spaghetti. Interviewers watch how candidates structure if/elif chains, and code review at any real company will call out nested conditionals deeper than two levels.
Common pitfalls
- Writing
if x == True:instead ofif x:. The explicit compare fails for truthy non-bool values like1or'yes'. - Using
=instead of==inside conditions. Python raisesSyntaxError, but the muscle memory from other languages persists. - Chaining comparisons wrong:
if 0 < x < 10is valid Python, butif x > 0 and < 10is a syntax error.
Indentation matters in Python. Code inside an if or a for is indented (4 spaces).
Pieces
if / elif / else— choose a pathfor x in range(a, b)— iterateaup to but not includingb%— the modulo operator (remainder of division)
Try it
- Extend FizzBuzz to 30.
- Add a "Bazz" for multiples of 7.
Practice
3 graded exercises
Write the code, click Check. We’ll tell you exactly what to fix.
Progress0 / 3
- Exercise 1
The variable
nis set. Print"even"if it's even, otherwise"odd". Test withn = 7. - Exercise 2
Print FizzBuzz for numbers 1 through 10 —
Fizzon multiples of 3,Buzzon multiples of 5,FizzBuzzon multiples of both, otherwise the number. - Exercise 3
Given
score = 82, print the grade:"A"if ≥90,"B"if ≥75, otherwise"C".