Skip to main content

Comparison operators

Note☆ Learning Objectives
  • Interpret what a comparison operation is.
  • Apply comparison operations (==, !=, >=, >, <=, <) accurately.

The comparison operators are used to determine how two values compare to eachother : Are these two values equal? Is the first value greater than this second value? Is the first value less than or equal to the second value? The result of the operation is a Boolean: True or False.

For instance, let’s say I borrowed 13 books at the library and I would like to check if I can borrow more given that the maximum is 15 books.

⏯️ Step by step Execution

MAX_BOOKS = 15    #Note this variable is capitalized because it is a constant.

my_book_count = 13

print("Can I borrow more books?")
print(my_book_count < MAX_BOOKS)

my_book_count +=1
print("Can I borrow more books?")
print(my_book_count < MAX_BOOKS)

my_book_count +=1
print("Can I borrow more books?")
print(my_book_count < MAX_BOOKS)

Adding Conditional Logic

Why use such operators? Those operators will come in handy when we introduce conditional statements (if,else). They allow a program to establish the relationship between values and make simple decisions. For example, if the age of the user is below 18, print “Sorry, you cannot vote”, otherwise print “You can vote”.

Equality operator ==

The double equal operator== compares two operands, if they are equal the result is True otherwise the result is False.

English Python
Did I borrow 14 books at the library? my_book_count == 14
Is the user’s name “Alice”? user_name == "Alice"

Inequality operator !=

Checks if the two oeprands are not equal. If they aren’t euqal, the result is True otherwise (if they’re equal) the result is False.

English Python
I put 10 cookies in my pantry, did my cookie count change? current_cookie_count != 10
I will go to work so long as it’s not my birthday. Will I go to work today? today != my_birthday

Greater than >

Checks if the left True if the left side operand is greater than the right side operand. Returns False otherwise.

English Python
Is Summer McIntosh the first Canadian to win that many gold medals in a single Summer Olympics? mcintosh_gold_count > best_canada_gold_count

Greater than or equal >=

Return True if the left side operand is greater or equal than the right side operand. Returns False otherwise.

English Python
The passing grade is 60, is Josh passing this course? josh_grade >= 60

Less than <

Return True if the left side operand is less than the right side operand. Returns False otherwise. - e.g.: 3 < 6 result: True , 3 < 2, result: False

English Python
Did Summer McIntosh set a new world record in the 400m butterfly? mc_intosh_time < world_record_butterfly

Less than or equal <=

Return True if the left side operand is less or equal than the right side operand. Returns False otherwise.

English Python
If the temperature outside below the freezing point? temperature <= 0.0

Summary of comparison operators

Comparison Operator Example Meaning Result
== 5 == 5 Is 5 equal to 5? True
!= 5 != 3 Is 5 not equal to 3? True
> 7 > 4 Is 7 greater than 4? True
< 2 < 6 Is 2 less than 6? True
>= 5 >= 5 Is 5 greater than or equal to 5? True
<= 3 <= 2 Is 3 less than or equal to 2? False

Note: Comparison operators compare two values and evaluate to a Boolean value: True or False.

Practice

🎯 Test Myself: Manually execute the operations below and write the expected result. Check your answers by running the code.

⏯️ Animate Execution

a = 11.999
b = 12
print(a < b)
a = (30 / 6) + 3
b = 2**3
print(a == b)
a = (25 // 10) * 10
b = 4**2 + 4
print(a != b)
a = 10
b = 7**2 +1
print(a >= b)

More practice questions on comparison operators are found in the conditionals section.