←Previous page

04CONDITIONALS

Ask. Then choose.

A conditional lets code choose a path after a yes-or-no question.

IN REAL LIFE

You decide like this every day.

A decision is a question with paths. Python does the same thing you do.

Rain or sun?

If it is raining, I play a board game inside. Else, I play soccer outside.

Hungry?

If I am hungry, I eat an apple. If not, I just keep playing.

Homework first

If my homework is done, I can play video games. Else, homework first.

LOOK FIRST

You already make choices this way.

if

If asks the question

“Is it raining?” Python checks whether the answer is true.

↳

The indented step is one path

If it is raining, the next step can be: take an umbrella.

else

Else is the other path

If the answer is no, Python can follow a different step.

if raining:
  print("umbrella")
take umbrella
OR
else:
  print("cap")
wear cap

STEP BY STEP

How Python decides.

Follow the code one line at a time, like PyBot does.

battery = 20A box keeps the number 20.
if battery < 30:Python asks: is 20 less than 30? Yes. The answer is True.
print("charge")True, so this indented line runs.
else:Skipped. The answer was already True.
print("play")Skipped too. Only one path runs.
print("bye")Not indented, so it always runs at the end.
sunny = FalseA yes-or-no box that keeps False.
if sunny:Python asks: is sunny True? No.
print("sunglasses")Skipped. There is no else, so this path just does nothing.
print("let's go")Not indented, so it always runs.

Three rules to remember.

Indentation shows the path

Lines pushed right with 4 spaces belong to the path. Lines on the left run no matter what.

The colon opens the path

if, elif, and else always end with : right before their path starts.

Only one path runs

Python never runs two paths of the same if. It picks one, then keeps going.

MORE THAN TWO PATHS

elif asks one more question.

Python asks from top to bottom and stops at the first True.

temperature = 15A box keeps 15 degrees.
if temperature > 25:Is 15 greater than 25? No. Go to the next question.
print("shorts")Skipped.
elif temperature > 10:Is 15 greater than 10? Yes!
print("jacket")This path runs.
else:Skipped. A path already ran.
print("coat")else runs only when every question was False.

RUN IT

Help PyBot check its battery.

Guess first. Then run real Python and check.

1

Guess the result

battery = 20 if battery < 30: print("charge") else: print("play")

What will Python show?

battery.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change 20 to a bigger number, like 80, so the other path runs. Run again.

YOUR TURN

Follow the true path.

Try each tiny question. A wrong answer becomes something to review.

0Completed
10Not tried
0Review
01

Park or movie?

Family rule: if it is sunny, we go to the park. Else, we watch a movie. Today it is raining.

What does the family do?

02

It is raining

raining = True
if raining:
  print("umbrella")

Which path runs?

03

Battery is low

battery_low = True
if battery_low:
  print("charger")

Which path runs?

04

It is not raining

raining = False
if raining:
  print("umbrella")
else:
  print("cap")

Which path does else choose?

05

No else here

sunny = False if sunny: print("sunglasses")

What does Python show?

06

The line on the left

raining = True if raining: print("umbrella") print("go out")

Which words show?

07

Three paths

temperature = 30 if temperature > 25: print("shorts") elif temperature > 10: print("jacket") else: print("coat")

Which path runs?

08

How many paths?

A program has an if and an else. The answer to the question is True.

How many paths run?

09

Fix PyBot's code

Something is missing at the end of the if line.

Goal output
Time to charge!
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

A conditional is a question with paths.

Python checks first. Then it follows the matching path.