✦ Python Foundations Fourth lesson

Concept 04 · Conditions

Conditions —
Teach Python to Make Decisions

Conditions let a program choose what to do next. Learn how Python checks an idea, follows one path, and skips the others.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

Conditions help Python choose a path.

A condition is an expression that Python can evaluate as True or False. In Operators, you learned that comparisons such as age >= 18 produce a Boolean result. Now if uses that result to choose which block of code runs: condition → True/False → choose a branch → run that block.

age = 20

if age >= 18:
    print("You are an adult")

Python first evaluates age >= 18 and gets True. Because the condition is True, Python enters the indented block and executes print(...). If the condition were False, Python would skip that block.

condition → True/False → choose a branch → run that block

The pattern if condition: starts a decision. if introduces the question, the condition produces a Boolean result, the colon announces a block, and indentation shows which code belongs to that block.

↳

Indentation matters: Indentation tells Python which statements belong to a branch. It is not only visual formatting: an if statement without an indented block, such as if age >= 18: print("Access granted"), raises an IndentationError.

Exactly one path with else
temperature = 12

if temperature > 20:
    print("It's warm")
else:
    print("It's cold")

An else block is the other path for the same question. Because temperature > 20 is False, Python skips the if block and runs else. Exactly one of these two branches runs.

Indentation defines the branch
if age >= 18:
    print("Adult")
    print("Access granted")

print("Program continues")

The first two print() lines are indented, so they belong to the if block. print("Program continues") is not indented, so it runs after the decision whether or not the if block ran.

elif checks another possibility
score = 75

if score >= 90:
    print("Excellent")
elif score >= 60:
    print("Passed")
else:
    print("Try again")

Python checks this if/elif/else chain from top to bottom. score >= 90 is False, so Python checks score >= 60. That is True, so it prints "Passed" and skips the remaining branch. elif is part of the same chain, not a separate if: the first True branch wins.

Put more specific conditions first
score = 95

# Too broad first
if score >= 60:
    print("Passed")
elif score >= 90:
    print("Excellent")

# More specific first
if score >= 90:
    print("Excellent")
elif score >= 60:
    print("Passed")

In the first chain, score >= 60 is already True, so Python prints "Passed" and never checks the elif. Python does not search for a better branch. When conditions overlap, put the more specific or stronger condition before the broader one.

Combine conditions
age = 20
has_ticket = True

if age >= 18 and has_ticket:
    print("Welcome!")
else:
    print("Access denied")

age >= 18 produces a Boolean result, while has_ticket is already a Boolean value. and combines those results, and if uses the final True or False to choose a branch. This is the Operators lesson applied to a decision.

Trace the flow before you run it
age = 16

print("Checking age...")

if age >= 18:
    print("Adult")
else:
    print("Minor")

print("Done")

Before you run this program, ask: Which lines always run? Which condition is checked? Which branch runs, and which branch is skipped? What is the complete output order? Here "Checking age..." runs first, the condition is False, so "Minor" runs, and "Done" runs last. This is the order in which the program follows its code.

Python Playground

Loading Python runtime…

Change the values of age and has_ticket. Before running the code, predict which branch Python will choose and why. Then run it and explain why the other branches were skipped.

conditions.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read the conditions from top to bottom. Python runs the first branch whose condition is true.

score = 70

if score >= 90:
    print("A")
elif score >= 60:
    print("B")
else:
    print("C")
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.