✦ 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

Conditions help Python choose a path.

A condition is an expression that is either <code>True</code> or <code>False</code>. Python uses that result to decide which block of code to run.

age = 20

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

The indented print() line runs only when age >= 18 is True. The colon starts the block, and indentation tells Python which code belongs inside it.

You are an adult

With if and else, Python has two possible paths. else is the alternative when the if condition is False.

↳

Indentation matters: Python uses indentation to define a block. An if statement without an indented block, such as if age >= 18: print("Access granted"), raises an IndentationError.

Choose between two paths
temperature = 12

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

Because temperature > 20 is False, Python skips the first block and runs the else block.

Check more than one possibility with elif
score = 75

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

Python checks an if/elif/else chain from top to bottom and runs the first matching branch. elif is part of the same chain, not a separate if.

Combine conditions
age = 20
has_ticket = True

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

Use and when both conditions must be true, or when at least one can be true, and not to reverse a true/false value. These are the Operators ideas applied to a decision.

Python Playground

Loading Python runtime…

Change the values of age and has_ticket. Try to predict which message Python will print.

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

Ask Sloby about conditions

Describe what feels unclear. Sloby can help you follow branches, indentation, and combined conditions.

Hi! I'm Sloby. Let's learn how Python makes decisions. Ask me about if, elif, else, or indentation!

What will this program print, and why? Explain which condition Python checks and why the other branches are skipped.

age = 17
has_permission = True

if age >= 18:
    print("Adult")
elif has_permission:
    print("Allowed with permission")
else:
    print("Not allowed")
Python AI Tutor · Learn by understanding, not just by running code.