Python20 min readBeginner

Control Flow & Loops

Make decisions and repeat work with if/elif/else, for, and while.

What is control flow?

By default, Python runs your code one statement at a time, top to bottom. "Control flow" is the set of language features that let you change that order — branch into different paths, repeat blocks, or skip them entirely. The two main tools are conditional statements (if/elif/else) and loops (for/while).

Comparison and boolean operators

Before we branch, we need a way to ask questions. Python's comparison operators return either `True` or `False` — values of type `bool`.

print(5 > 3)        # True
print(5 == 5)       # True (equality is two equals signs)
print(5 != 6)       # True
print("a" in "cat") # True (membership)

# Combining conditions
age = 25
print(age >= 18 and age < 65)  # True (both must be true)
print(age < 13 or age > 65)    # False (at least one must be true)
print(not (age == 25))         # False (flips it)

if / elif / else

The `if` statement runs a block only if a condition is true. `elif` ("else if") chains additional conditions, and `else` runs when nothing else matched. Note the colons at the end of each line and the indentation — Python uses indentation to mark which lines belong to which block. The standard is 4 spaces.

score = 78

if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
else:
    grade = "F"

print(grade)

Python also has a one-line conditional expression (sometimes called the ternary operator). It's handy when you just want to pick between two values:

age = 20
status = "adult" if age >= 18 else "minor"
print(status)

for loops

A `for` loop repeats a block once for each item in some collection (list, string, range, etc.). The variable after `for` takes on each value in turn.

# Loop over a range of numbers (0 through 4)
for i in range(5):
    print(i)

# Loop over the characters in a string
for letter in "hello":
    print(letter)

# Loop over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

`range(n)` produces 0, 1, 2, ..., n-1. `range(start, stop)` produces start, start+1, ..., stop-1. `range(start, stop, step)` skips by `step` each time. This is one of the most-used functions in all of Python.

while loops

A `while` loop keeps running as long as a condition is true. Use it when you don't know in advance how many iterations you need.

n = 5
while n > 0:
    print(n)
    n -= 1   # shorthand for n = n - 1
print("Liftoff!")
⚠ Watch out
If the condition never becomes false, your loop runs forever and freezes your program. Always make sure something inside the loop changes the variables in the condition.

break and continue

`break` exits the loop immediately. `continue` skips the rest of the current iteration and goes to the next one. Both work in `for` and `while`.

for n in range(20):
    if n == 10:
        break          # stop entirely at 10
    if n % 2 == 1:
        continue       # skip odd numbers
    print(n)
What does range(2, 8, 2) produce?