Decisions and loops
Comparisons and Boolean logic
Create Boolean expressions with comparisons, and combine them with and, or, and not.
Lesson 13 of 63
Reading: about 2 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page4 sections
A comparison produces True or False. Use == to test equality; = assigns a value. Other comparison operators include !=, <, <=, >, and >=.
minutes = 25
print(minutes >= 20)
print(minutes == 30)
print(minutes != 30)True False True
Combine conditions
and is True only when both conditions are True. or is True when either is True. not reverses a Boolean value. Parentheses make grouped conditions easier to read.
minutes = 25
print(minutes >= 20 and minutes <= 30)
print(minutes < 10 or minutes > 60)
print(not (minutes == 0))True False True
Why this works
A comparison is an expression whose value is True or False. Python evaluates a combined condition from left to right: and can skip the right side when the left is False, while or can skip it when the left is True. Parentheses make the intended grouping visible when a condition has several parts.
Practice
Set score to 75. Print whether it is at least 50, whether it equals 100, and whether it is between 50 and 100 inclusive.
Which operator tests whether two values are equal?
Reveal answer
Use ==. A single = assigns a value.
Try another case
With minutes = 25, what is the value of minutes < 20 or minutes == 25?
Check your answer
Reveal answer
True. The first comparison is False, but the second is True, so or produces True.
Your progress
Reveal the check answer or reach the end to complete this lesson.