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.
12print(5 == 5) # True print("apple" == "Apple") # False (case matters)
=
assigns a value to a variable, while ==
compares two values.
Inequality β !=
Checks whether two values are different.
12print(5 != 3) # True print("cat" != "cat") # False
Greater Than β >
True if the left value is strictly larger than the right.
12print(7 > 9) # False print(12 > 3) # True
Less Than β <
True if the left value is strictly smaller than the right.
12print(2 < 10) # True print("a" < "b") # True (lexicographic order)
Greater Than or Equal β >=
True if the left value is larger or equal to the right.
12print(7 >= 7) # True print(4 >= 9) # False
Less Than or Equal β <=
True if the left value is smaller or equal to the right.
12print(10 <= 9) # False print(5 <= 5) # True
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)
.
123x = 7 print(0 < x < 10) # True print(5 <= x <= 7) # True
This reads cleanly and avoids repeating x
.
Floating-point nuance (tiny rounding errors)
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 (case and order)
String comparisons are case-sensitive and lexicographic (character-by-character in Unicode order).
12print("apple" == "Apple") # False (case matters) print("apple" < "banana") # True ("a" comes before "b")
For case-insensitive checks, normalize both sides first.
12s1, s2 = "Hello", "heLLo" print(s1.lower() == s2.lower()) # True
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
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.
12print(5 == 5) # True print("apple" == "Apple") # False (case matters)
=
assigns a value to a variable, while ==
compares two values.
Inequality β !=
Checks whether two values are different.
12print(5 != 3) # True print("cat" != "cat") # False
Greater Than β >
True if the left value is strictly larger than the right.
12print(7 > 9) # False print(12 > 3) # True
Less Than β <
True if the left value is strictly smaller than the right.
12print(2 < 10) # True print("a" < "b") # True (lexicographic order)
Greater Than or Equal β >=
True if the left value is larger or equal to the right.
12print(7 >= 7) # True print(4 >= 9) # False
Less Than or Equal β <=
True if the left value is smaller or equal to the right.
12print(10 <= 9) # False print(5 <= 5) # True
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)
.
123x = 7 print(0 < x < 10) # True print(5 <= x <= 7) # True
This reads cleanly and avoids repeating x
.
Floating-point nuance (tiny rounding errors)
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 (case and order)
String comparisons are case-sensitive and lexicographic (character-by-character in Unicode order).
12print("apple" == "Apple") # False (case matters) print("apple" < "banana") # True ("a" comes before "b")
For case-insensitive checks, normalize both sides first.
12s1, s2 = "Hello", "heLLo" print(s1.lower() == s2.lower()) # True
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!