← Back to Programming

Making decisions

Programs become useful when they can react differently to different situations. Python makes decisions with conditions and if statements.

Comparison operators

Comparisons produce Booleans: == (equal), != (not equal), <, >, <=, and >=. Note that == compares, while = assigns; mixing them up is a common beginner mistake. Strings compare alphabetically by character codes, so "apple" < "banana" is True.

if statements

An if statement runs an indented block only when its condition is True: if temperature > 30: print("Hot!"). The colon starts the block, and indentation, conventionally four spaces, shows which lines belong to it. Python uses indentation instead of braces, so consistent spacing matters.

else and elif

else provides an alternative when the condition is False. elif, short for else-if, tests further conditions in order; Python runs the first block whose condition is True and skips the rest. For grades: if score >= 90: grade = "A" elif score >= 80: grade = "B" else: grade = "C".

Logical operators

and is True only if both sides are; or is True if at least one is; not flips a Boolean. if age >= 13 and age <= 19 checks for a teenager; Python also allows the chained form 13 <= age <= 19. Python evaluates and and or lazily, stopping as soon as the answer is known.

Truthiness

In conditions, Python treats some values as false: False, 0, 0.0, empty strings, empty lists, and None. Everything else counts as true. So if name: means 'if name is not empty'. This is convenient but can surprise beginners, for example when 0 is a valid answer.

Nested conditions

if statements can contain other if statements, but deep nesting is hard to read. Often it is clearer to combine conditions with and, or to return early from a function. Writing conditions in a logical order, most specific first, prevents later branches from being unreachable.

match statements

Python 3.10 added match statements for comparing a value against several patterns: match command: case "start": ... case "stop": ... case _: ... The underscore matches anything, like a default. match can also unpack structures, though if-elif chains remain the most common choice for simple decisions.

Worked example

A leap-year check: year = int(input()); if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0: print("Leap year") else: print("Not a leap year"). For 2024: divisible by 4, not by 100, so leap. For 1900: divisible by 100 but not 400, so not leap. For 2000: divisible by 400, so leap.

Key takeaways

Comparison operators produce Booleans; == compares and = assigns. if runs an indented block when a condition is True; indentation defines blocks. elif and else handle alternatives; only the first true branch runs. and, or, and not combine conditions; chained comparisons are allowed. Empty values, zero, and None are falsy. Avoid deep nesting. match statements (Python 3.10+) compare against patterns.

Flashcard

==

Equality comparison

Not assignment.

Check your understanding

With score = 85, which grade does this code assign: if score >= 90: 'A' elif score >= 80: 'B' elif score >= 70: 'C'?

← Previous lesson Next lesson →