Rain or sun?
If it is raining, I play a board game inside. Else, I play soccer outside.
04CONDITIONALS
A conditional lets code choose a path after a yes-or-no question.
IN REAL LIFE
A decision is a question with paths. Python does the same thing you do.
If it is raining, I play a board game inside. Else, I play soccer outside.
If I am hungry, I eat an apple. If not, I just keep playing.
If my homework is done, I can play video games. Else, homework first.
LOOK FIRST
“Is it raining?” Python checks whether the answer is true.
If it is raining, the next step can be: take an umbrella.
If the answer is no, Python can follow a different step.
if raining:
print("umbrella")take umbrellaelse:
print("cap")wear capSTEP BY STEP
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.Lines pushed right with 4 spaces belong to the path. Lines on the left run no matter what.
if, elif, and else always end with : right before their path starts.
Python never runs two paths of the same if. It picks one, then keeps going.
MORE THAN TWO PATHS
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
Guess first. Then run real Python and check.
battery = 20
if battery < 30:
print("charge")
else:
print("play")What will Python show?
PyBot's hint
Ready. Press Run Python.
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
Try each tiny question. A wrong answer becomes something to review.
What does the family do?
raining = True
if raining:
print("umbrella")Which path runs?
battery_low = True
if battery_low:
print("charger")Which path runs?
raining = False
if raining:
print("umbrella")
else:
print("cap")Which path does else choose?
sunny = False
if sunny:
print("sunglasses")What does Python show?
raining = True
if raining:
print("umbrella")
print("go out")Which words show?
temperature = 30
if temperature > 25:
print("shorts")
elif temperature > 10:
print("jacket")
else:
print("coat")Which path runs?
How many paths run?
Something is missing at the end of the if line.
Time to charge!OUTPUT
Want a fresh start? Clear only this page.
Python checks first. Then it follows the matching path.