Python course Β· Module 1: Python Basics
Control Flow - Choosing a Path in the Jungle
In this lesson6
Welcome! We stand at a fork in the trails. One path leads across a river, the other through dense forest. A good explorer must make decisions based on conditions. In Python, conditional statements are used to make decisions: if, elif, and else.
The if Statement - If the Condition Is Met
The word if checks a condition. If the condition is True, Python executes the indented block of code. If it's False - it skips it.
temperature = 35
if temperature > 30:
print("It's hot! Stay in the shade.")
print("Remember to drink water.")Notice three things: after the condition we put a colon :, and the code to execute is indented (usually 4 spaces). This indentation tells Python: "this belongs to the condition." A condition is an expression that returns True or False - exactly what you learned with comparison operators.
if / else - Two Paths
Often we want to do one thing when a condition is met and something else when it's not. That's what else ("otherwise") is for.
water_supply = 2 # liters
if water_supply >= 3:
print("We have enough water for today.")
else:
print("Low on water! We must find a source.")Python checks the condition at if. If it's True - it runs the if block. If False - it jumps to the else block. Exactly one of the two paths always runs.
if / elif / else - Multiple Paths
But what if there are more than two paths? Imagine evaluating temperature: hot, pleasant, or cold. That's what elif is for (short for "else if" - "otherwise if"). You can add as many elif blocks as you need.
temperature = 22
if temperature > 30:
print("It's hot in the jungle!")
elif temperature >= 15:
print("Pleasant temperature for marching.")
elif temperature >= 5:
print("Chilly, grab a jacket.")
else:
print("Cold! Light a campfire.")Python checks conditions one by one, top to bottom. It runs the block of the first condition that is True and skips the rest. If no condition matches, it runs the else block (if one exists). That's why order matters - we check from the most specific conditions.
Combining Conditions in Decisions
Remember the logical operators and, or, not? They work great inside conditional statements:
temperature = 28
is_raining = False
if temperature > 20 and not is_raining:
print("Perfect conditions - off on safari!")
else:
print("We stay in camp.")Practice
Write a program that evaluates whether an animal is dangerous based on its weight. Run the code in the editor and change the value of weight (e.g. to 190 or 5) to see which branch runs:
Code for this lesson: main.py
# Control flow - is the animal dangerous?
animal = "Elephant"
weight = 5000 # kg
# TODO: complete the if / elif / else conditions
if weight > 1000:
print(f"{animal} is a large animal - keep your distance!")
elif weight >= 100:
print(f"{animal} - medium-sized, be careful.")
else:
print(f"{animal} - small animal, safe.")
# Try changing weight to 190 or 5 and run again!Summary
In this lesson you learned to make decisions in code:
if- runs code when the condition isTrueelse- runs code when the condition isFalseelif- checks further conditions when previous ones wereFalse- The meaning of the colon
:and indentation in blocks - Combining conditions with
and,or,not
In the next lesson Darwin will teach you loops - how to repeat actions without rewriting code!
Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. When does the indented block after if run?
2. What is elif used for?
3. What do we put at the end of the if condition line?
Hands-on tasks in the game
- Vertical ordering
Order the conditional statement: