BaseCodeByte
Back to Python course
PyControl FlowBeginnerLive Python11 min

If Statements

Run code only when a condition is true.

Explanation cell

An if statement runs code only when a condition is true. This lets programs make decisions.

ifcomparisondecision-making

Quick tip

Python runs an indented block only when the condition is true.

Common mistake

Forgetting the colon after the if line is one of the most common beginner errors.

Code cell

Starter code · Ctrl+Enter runs · Ctrl+Shift+Enter tests

Python runtime loads on first run

Output cell

Run your code to see output.

Run your code to see output.

Test cell

Tests run against the actual Python output.

Run tests to check your code against the lesson goals.

Practice cell

Try this

Add an else branch that prints "Try again".

Expected output

Pass