Comparison Operators
Comparisons let your program ask yes/no questions about values:
- Are these the same?
- Is this bigger?
- Does this number fall inside a range?
A comparison returns a Boolean (True or False) and is the backbone of if/while logic.
The Essentials
Python provides six comparison operators (==, !=, <, <=, >, >=) to test equality and ordering between values, each comparison evaluates to True or False.
Equality ==
Checks whether two values are the same.
1234567saved_pin = 1234 entered_pin = 1234 print(saved_pin == entered_pin) # True β user entered the correct PIN stored_email = "support@codefinity.com" input_email = "Support@codefinity.com" print(stored_email == input_email) # False β case matters in string comparison
= assigns a value to a variable, while == compares two values.
Inequality !=
Checks whether two values are different.
1234567user_id_1 = 105 user_id_2 = 203 print(user_id_1 != user_id_2) # True β users have different IDs username_1 = "alex" username_2 = "alex" print(username_1 != username_2) # False β usernames match
Greater Than >
True if the left value is strictly larger than the right.
123456789# Comparing delivery times in minutes estimated_time = 7 actual_time = 9 print(estimated_time > actual_time) # False β delivery took longer than expected # Comparing two product ratings rating_product_a = 12 rating_product_b = 3 print(rating_product_a > rating_product_b) # True β product A has a higher rating
Less Than <
True if the left value is strictly smaller than the right.
123456789# Comparing user's age with the minimum required age user_age = 17 min_age = 18 print(user_age < min_age) # True β user is too young to access the service # Comparing names alphabetically first_name = "Alice" second_name = "Bob" print(first_name < second_name) # True β "Alice" comes before "Bob" alphabetically
Greater Than or Equal >=
True if the left value is larger or equal to the right.
123456789# Checking if a student reached the passing score student_score = 7 passing_score = 7 print(student_score >= passing_score) # True β student passed the test # Comparing two package weights before shipping package_weight = 4 min_weight_required = 9 print(package_weight >= min_weight_required) # False β package is too light
Less Than or Equal <=
True if the left value is smaller or equal to the right.
123456789# Checking if an order total qualifies for a discount limit order_total = 10 discount_limit = 9 print(order_total <= discount_limit) # False β total exceeds the discount limit # Verifying if a student arrived on time (in minutes) arrival_time = 5 deadline_time = 5 print(arrival_time <= deadline_time) # True β student arrived right on time
Chained Comparisons
Python lets you write ranges naturally:
0 < x < 10 means "x is greater than 0 and less than 10".
Under the hood it behaves like (0 < x) and (x < 10).
1234567# Checking if the temperature is within a comfortable range temperature = 7 print(0 < temperature < 10) # True β temperature is within the cool range # Checking if a user's rating fits the top-tier range user_rating = 7 print(5 <= user_rating <= 7) # True β rating is within the premium bracket
This reads cleanly and avoids repeating x.
Floating-Point Nuance
Some decimals (like 0.1) cannot be represented exactly in binary. That's why strict equality on floats can surprise you.
1print(0.1 + 0.2 == 0.3) # False in many environments
When comparing floats for "equality", prefer a tolerance check.
12import math print(math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9)) # True
You're not saying "exactly equal", you're saying "close enough".
Comparing Strings
String comparisons are case-sensitive and lexicographic (character-by-character in Unicode order).
123456789# Comparing user input with stored data saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False β passwords are case-sensitive # Sorting items alphabetically first_item = "apple" second_item = "banana" print(first_item < second_item) # True β "apple" comes before "banana" alphabetically
For case-insensitive checks, normalize both sides first.
12345# Comparing email addresses entered in different cases email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True β emails match, case ignored
1. Fill in the blanks with True or False:
2. Which single expression correctly checks that x is between 1 and 5 inclusive (using chaining)?
3. Which string comparison is True?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5.26
Comparison Operators
Swipe to show menu
Comparisons let your program ask yes/no questions about values:
- Are these the same?
- Is this bigger?
- Does this number fall inside a range?
A comparison returns a Boolean (True or False) and is the backbone of if/while logic.
The Essentials
Python provides six comparison operators (==, !=, <, <=, >, >=) to test equality and ordering between values, each comparison evaluates to True or False.
Equality ==
Checks whether two values are the same.
1234567saved_pin = 1234 entered_pin = 1234 print(saved_pin == entered_pin) # True β user entered the correct PIN stored_email = "support@codefinity.com" input_email = "Support@codefinity.com" print(stored_email == input_email) # False β case matters in string comparison
= assigns a value to a variable, while == compares two values.
Inequality !=
Checks whether two values are different.
1234567user_id_1 = 105 user_id_2 = 203 print(user_id_1 != user_id_2) # True β users have different IDs username_1 = "alex" username_2 = "alex" print(username_1 != username_2) # False β usernames match
Greater Than >
True if the left value is strictly larger than the right.
123456789# Comparing delivery times in minutes estimated_time = 7 actual_time = 9 print(estimated_time > actual_time) # False β delivery took longer than expected # Comparing two product ratings rating_product_a = 12 rating_product_b = 3 print(rating_product_a > rating_product_b) # True β product A has a higher rating
Less Than <
True if the left value is strictly smaller than the right.
123456789# Comparing user's age with the minimum required age user_age = 17 min_age = 18 print(user_age < min_age) # True β user is too young to access the service # Comparing names alphabetically first_name = "Alice" second_name = "Bob" print(first_name < second_name) # True β "Alice" comes before "Bob" alphabetically
Greater Than or Equal >=
True if the left value is larger or equal to the right.
123456789# Checking if a student reached the passing score student_score = 7 passing_score = 7 print(student_score >= passing_score) # True β student passed the test # Comparing two package weights before shipping package_weight = 4 min_weight_required = 9 print(package_weight >= min_weight_required) # False β package is too light
Less Than or Equal <=
True if the left value is smaller or equal to the right.
123456789# Checking if an order total qualifies for a discount limit order_total = 10 discount_limit = 9 print(order_total <= discount_limit) # False β total exceeds the discount limit # Verifying if a student arrived on time (in minutes) arrival_time = 5 deadline_time = 5 print(arrival_time <= deadline_time) # True β student arrived right on time
Chained Comparisons
Python lets you write ranges naturally:
0 < x < 10 means "x is greater than 0 and less than 10".
Under the hood it behaves like (0 < x) and (x < 10).
1234567# Checking if the temperature is within a comfortable range temperature = 7 print(0 < temperature < 10) # True β temperature is within the cool range # Checking if a user's rating fits the top-tier range user_rating = 7 print(5 <= user_rating <= 7) # True β rating is within the premium bracket
This reads cleanly and avoids repeating x.
Floating-Point Nuance
Some decimals (like 0.1) cannot be represented exactly in binary. That's why strict equality on floats can surprise you.
1print(0.1 + 0.2 == 0.3) # False in many environments
When comparing floats for "equality", prefer a tolerance check.
12import math print(math.isclose(0.1 + 0.2, 0.3, rel_tol=1e-9)) # True
You're not saying "exactly equal", you're saying "close enough".
Comparing Strings
String comparisons are case-sensitive and lexicographic (character-by-character in Unicode order).
123456789# Comparing user input with stored data saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False β passwords are case-sensitive # Sorting items alphabetically first_item = "apple" second_item = "banana" print(first_item < second_item) # True β "apple" comes before "banana" alphabetically
For case-insensitive checks, normalize both sides first.
12345# Comparing email addresses entered in different cases email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True β emails match, case ignored
1. Fill in the blanks with True or False:
2. Which single expression correctly checks that x is between 1 and 5 inclusive (using chaining)?
3. Which string comparison is True?
Thanks for your feedback!