Python / Making decisions

if - else and comparison operators

Choose between two paths with if - else, and compare values: equal, not equal, greater, less. Baccalaureate

An if on its own does nothing when the condition fails. But often you want a reply either way: "if you passed, congratulations, otherwise try again". That is what else is for.

Python
books = 80
if books == 100:
    print("Reading Champion Badge!")
else:
    print("Let's try a little more!")
Console
Let's try a little more!

Exactly one of the two paths runs: the if block when the condition is True, the else block when it is False.

else: starts at the beginning of the line (level with the if), ends with a colon, and never has a condition after it.

Comparison operators

A condition is not only "equals". You can ask: bigger? smaller? not equal?

OperatorMeaningExample (age = 15)Result
==equal toage == 15True
!=not equal toage != 15False
>greater thanage > 15False
<less thanage < 18True
>=greater than or equal toage >= 15True
<=less than or equal toage <= 16True
Python
english = 70
if english >= 65:
    print("Pass")
else:
    print("Fail")
Console
Pass
> does not include the value itself; >= does: 65 > 65 is False, 65 >= 65 is True. The = always comes after > or <; => is an error.

Comparing text

Text can be compared with == and != too. Careful: "not rainy" does not mean "sunny"; a cloudy day is not rainy either:

Python
weather = "Cloudy"
if weather != "Rainy":
    print("Play soccer")
else:
    print("Stay inside")
Console
Play soccer

Practice

1Write the programCurriculum practice

Assign 777 to lucky. If lucky equals 777, print Lucky!; otherwise print Try again.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
2Edit the programCurriculum practice

Change the condition so it prints Practice worksheet when chem is 40 or less, otherwise No worksheet.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
chem = 28
if chem == 40:
    print("Practice worksheet")
else:
    print("No worksheet")
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Complete the condition: if height is greater than 132, print Available; otherwise Unavailable.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
height = 91
if ___:
    print("Available")
else:
    print("Unavailable")
Console
Press Run to see the result.
4Write the programCurriculum practice

Assign 15 to age. If age is 65 or more, print Slide closed; otherwise print Slide OK.

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
5Fill in the blankCurriculum practice

Complete the condition: if myScore is not equal to 100, print Aim for a perfect score!; otherwise print Perfect!

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
myScore = 90
if ___:
    print("Aim for a perfect score!")
else:
    print("Perfect!")
Console
Press Run to see the result.
6Write the programExtra practice

Assign 17 to number. If its remainder when divided by 2 is 0, print Even; otherwise print Odd.

HintThe remainder operator is %.
Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

The school trip costs 350 pounds and Sara has 280 in money. If money is enough (350 or more), print You can join the trip; otherwise print what is missing as You need 70 more (calculate it in code).

Python Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • if condition: then else:; exactly one path runs, and else has no condition.
  • Comparison operators: == != > < >= <=; >= includes the value itself.
  • Text can be compared with == and !=.