Everyone Can Learn Program
中文 Start learning
Course 2 · Python BasicsLesson 7 of 14Free to read

if and else · Python from Zero

How does a program choose between two paths?

Course contents

Why 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:

OperatorMeans
==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 yourself

Try 18, 17 and 40 in the Input box.

age = int(input())
if age >= 18:
    print("adult")
else:
    print("minor")
print("checked")
Input (one line per input() call)
17
Step by step
  1. L1age = 17age = 17
  2. L2Is 17 >= 18? False.False
  3. L5So the else block runs.minor
  4. L6This line is outside the if/else: it always runs.checked

Comparisons are values

Try it yourself

Print comparisons directly to see True and False.

print(3 < 5, 3 == 3.0)
print("a" == "A", 10 != 10)
Step by step
  1. L13 is less than 5; 3 and 3.0 are equal numbers.True True
  2. 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")
  1. A
    big
    done
  2. B
    big
  3. C
    small
    done
Show the answer

Answer: A

7 > 5 is True, so "big"; then "done" always runs.

2. What does this print?

print(10 == "10")
  1. A
    True
  2. B
    False
  3. C
    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.

  1. PredictWhich branch?

    Type exactly what this program prints.

  2. ModifyAdd an else

    The program only says something for positive numbers. Add an else that prints not positive.

  3. 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

TermChineseDefinition
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.