Skip to content
Topic 4 of 6

Control Flow

Make decisions with if, elif and else, truthy and falsy values, and chained comparisons.

20 min read 4 pages 5 practice games Jump to practice ↓

Page 1

if, elif and else

An if statement runs a block of code only when its condition is true. The condition is any expression; Python checks whether it is true or false.

python
age = 17
if age >= 18:
    print("You can vote")
else:
    print("Not yet")   # prints: Not yet

The else block runs when the if condition is false. An else has no condition of its own.

Use elif (short for "else if") to test more conditions in order. Python checks the conditions from top to bottom and runs only the first block whose condition is true; the rest are skipped. You can have any number of elif blocks, and else is optional.

python
score = 72
if score >= 90:
    grade = "A"
elif score >= 70:
    grade = "B"
elif score >= 50:
    grade = "C"
else:
    grade = "F"
print(grade)   # B

Every if, elif and else line ends with a colon, and the block under it is indented.

Page 2

Truthiness: truthy and falsy values

A condition doesn't have to be True or False. Python can test the truth value of any object. Values that count as false are called falsy; everything else is truthy.

The falsy values are False, None, zero of any number type (0 and 0.0), the empty string "", and empty collections: an empty list [], an empty tuple (), an empty dictionary {}, an empty set set() and range(0).

Every other value is truthy, including non-empty strings such as "0" and "False", negative numbers such as -1, and a list that contains something.

python
name = ""
if name:
    print("Hi", name)
else:
    print("No name given")   # runs: an empty string is falsy

The built-in function bool() shows the truth value of any object: bool(0) is False and bool("hello") is True.

Page 3

Comparisons, logic and comparison chaining

Conditions are usually built from comparison operators: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to) and >= (greater than or equal to). Each comparison gives a bool, True or False. Don't confuse == (comparison) with = (assignment).

Combine conditions with the logical operators and, or and not. and is true only if both sides are true; or is true if at least one side is true; not flips a truth value.

Python lets you chain comparisons; this is called comparison chaining. The chain 0 <= x < 10 means the same as 0 <= x and x < 10, but x is evaluated only once.

python
x = 7
print(0 <= x < 10)   # True
print(1 < x > 5)     # True: means 1 < x and x > 5

Python checks and and or with short-circuit evaluation: it stops as soon as the result is known. In a and b, if a is false, b is never evaluated.

Page 4

Nested conditions

An if statement can sit inside another if block. This is called a nested condition. The inner block is indented one more level.

python
logged_in = True
is_admin = False
if logged_in:
    if is_admin:
        print("Admin panel")
    else:
        print("Welcome back")   # runs
else:
    print("Please log in")

Deep nesting is hard to read. You can often flatten it: if logged_in and is_admin: does the same job as two nested ifs, and elif can replace an if nested inside an else.

Finished reading?

Mark it as read for +20 XP. Optional: practice is open either way.

Practice

Pass any one game to unlock Topic 5: Loops.

Sign in to play, save your best and unlock Topics.
  • Reach −1,500 m
  • Reach 150 km
  • 7/10 without falling
  • Clear both boards
  • 150 points

Learn more

Control Flow · Python Basics Topic 4 · SYLLABYSS