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:
| Operator | Meaning | True example |
|---|---|---|
== | equal to | 3 == 3 |
!= | not equal to | 3 != 4 |
< | strictly less than | 2 < 5 |
<= | less than or equal to | 5 <= 5 |
> | strictly greater than | 7 > 1 |
>= | greater than or equal to | 7 >= 7 |
You can also compare text: "Lagos" == "Lagos" is True, and the comparison is case-sensitive.
Combining: and, or, not
A and Bis true only if both are true.A or Bis true if at least one of the two is true.not Ainverts the value:not TrueisFalse.
The program displays the result.
Text description of the animation
- To rent a bike, you must be at least 14 and have a helmet.
- age is 16.
- helmet is False: no helmet today.
- Python first evaluates the left-hand side: the age condition is true.
- and requires both to be true: the result is False.
- We store False in allowed.
- 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
(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
andis evaluated beforeor, but parentheses make your intention readable. - Writing
year % 4 = 0: that's an assignment, so Python raises aSyntaxError.
Summary
- 1The comparisons ==, !=, <, <=, >, >= produce a boolean.
- 2and requires both conditions to be true, or requires at least one.
- 3not inverts a condition.
- 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.