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?
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain more about how chained comparisons work in Python?
What are some common pitfalls when comparing floating-point numbers?
How do I perform a case-insensitive string comparison in Python?
Awesome!
Completion rate improved to 5
Comparison Operators
Scorri per mostrare il 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?
Grazie per i tuoi commenti!