Skip to content
KALANBLON
Show contents

Chapter 2 of 6 · 15 min

Comparing and combining conditions

By the end of this chapter, you'll be able to

  • Use the six comparison operators
  • Combine conditions with and and or
  • Invert a condition with not
  • Tell = (assignment) and == (comparison) apart

Comparison operators

A comparison operator compares two values and produces a boolean:

OperatorMeaningTrue example
==equal to3 == 3
!=not equal to3 != 4
<strictly less than2 < 5
<=less than or equal to5 <= 5
>strictly greater than7 > 1
>=greater than or equal to7 >= 7

You can also compare text: "Lagos" == "Lagos" is True, and the comparison is case-sensitive.

Combining: and, or, not

  • A and B is true only if both are true.
  • A or B is true if at least one of the two is true.
  • not A inverts the value: not True is False.

The program displays the result.

Step 7 of 7
Text description of the animation
  1. To rent a bike, you must be at least 14 and have a helmet.
  2. age is 16.
  3. helmet is False: no helmet today.
  4. Python first evaluates the left-hand side: the age condition is true.
  5. and requires both to be true: the result is False.
  6. We store False in allowed.
  7. The program displays the result.
age = 16
student = False
helmet = False
print("Bike:", age >= 14 and helmet)
print("Free museum:", student or age < 18)
print("No helmet:", not helmet)
Bike: False
Free museum: True
No helmet: True

You can use these combinations directly in an if:

hour = 22
weekend = True
if hour >= 21 and not weekend:
    print("Bedtime, there's school tomorrow!")
else:
    print("One more episode.")
One more episode.

Your turn

Mini-lab: the passwordPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: the leap year

A year is a leap year if it's divisible by 4 and not by 100, or if it's divisible by 400. Write a program that displays True or False for year = 2024, then also test 1900 and 2000.

Hint 1
year % 4 == 0 is true when year is divisible by 4: % gives the remainder of the division.
Hint 2
Use parentheses to group: (A and B) or C.
Show the solution and common mistakes
for year in [2024, 1900, 2000]:
    leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
    print(year, leap)
2024 True
1900 False
2000 True

(The for loop lets you test the three years in one go: you'll discover it in detail in the next part.)

Common mistakes:

  • Forgetting the parentheses: the result is still correct here because and is evaluated before or, but parentheses make your intention readable.
  • Writing year % 4 = 0: that's an assignment, so Python raises a SyntaxError.

Summary

  1. 1The comparisons ==, !=, <, <=, >, >= produce a boolean.
  2. 2and requires both conditions to be true, or requires at least one.
  3. 3not inverts a condition.
  4. 4= stores a value, == compares two values.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.