1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Comparisons

A comparison asks a yes/no question and gives back a bool:

5 > 3      # True
5 == 3     # False  (== compares, = assigns!)
5 != 3     # True
age >= 18

< and > exclude equality; <= and >= include it.

Python can chain comparisons, as in maths: 13 <= age <= 19.

See it run

price = 20
budget = 20
print(price < budget)
print(price <= budget)
print(price == budget)
print(15 <= price <= 25)

Every comparison turns into a bool. Watch the Variables box: nothing changes, only the answers do.

Practice comparisons

Exercise 1

With age = 17:

  • can_vote = whether age is at least 18
  • is_teen = whether age is between 13 and 19 (use a chained comparison)

Exercise 2

A fridge is safe from 2°C through 5°C, including both ends. With temperature = 5, set boolean safe using a chained comparison.

Exercise 3

A parcel locker accepts weights from 0.5 kg up to, but not including, 10 kg. With weight = 10, set fits with one chained comparison and too_heavy for weights of 10 kg or more.

Free lesson: use the editor to practice it.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest