if and else · Python from Zero
How does a program choose between two paths?
Course contentsWhy you need it
Until now every line ran every time. Real programs react: a login checks the password, a shop checks the stock, a game checks if you've won. To react, a program compares values and runs different code depending on the answer.
The intuition
You did this with the robot: if on_gem():. Now the question is a comparison you write yourself:
| Operator | Means |
|---|---|
== | equal to |
!= | not equal to |
< > | less / greater than |
<= >= | less / greater than or equal |
Each comparison gives True or False.
if age >= 18:
print("adult")
else:
print("minor")Exactly one of the two blocks runs. Lines after the whole if/else (not indented) always run.
Definition
Comparison operator
A comparison operator such as ==, !=, < or >= compares two values and produces True or False.
if / else
An if statement runs its block when the condition is True; the optional else block runs when it is False.
Examples
Adult or minor
Try it yourselfTry 18, 17 and 40 in the Input box.
age = int(input())
if age >= 18:
print("adult")
else:
print("minor")
print("checked")17
- L1age = 17
age = 17 - L2Is 17 >= 18? False.
False - L5So the else block runs.
minor - L6This line is outside the if/else: it always runs.
checked
Comparisons are values
Try it yourselfPrint comparisons directly to see True and False.
print(3 < 5, 3 == 3.0)
print("a" == "A", 10 != 10)- L13 is less than 5; 3 and 3.0 are equal numbers.
True True - L2Strings compare exactly, so case matters. 10 != 10 is False.
False False
Common mistakes
= instead of ==
if x = 5: is a SyntaxError. One = stores, two == compare.
Comparing text with numbers
input() gives text, and "5" == 5 is False. Convert with int() before comparing.
Quick quiz
1. What does this print?
x = 7
if x > 5:
print("big")
else:
print("small")
print("done")Show the answer
Answer: A
7 > 5 is True, so "big"; then "done" always runs.
2. What does this print?
print(10 == "10")Show the answer
Answer: B
A number is never equal to a string, even if they look the same.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictWhich branch?
Type exactly what this program prints.
- ModifyAdd an else
The program only says something for positive numbers. Add an
elsethat printsnot positive. - WriteThe larger number
Read two whole numbers and print the larger one. Use
if; don't use max(). If they're equal, print that number.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Comparison operator | 比较运算符 | An operator such as == or < that compares two values and gives True or False. |
| Branch | 分支 | One of the alternative blocks of code that an if statement can run. |
| Equality == | 相等 == | The operator that checks whether two values are equal. |