In programming, we often need to compare values to make decisions. Just as we use arithmetic operators like +
and -
to perform calculations, Python provides comparison operators (also known as relational operators) to check the relationship between two values. These comparisons are fundamental for controlling the flow of a program, allowing scripts to react differently based on specific conditions.
The result of any comparison operation is always a Boolean value: either True
or False
. Think of it as asking Python a yes-or-no question about the relationship between two pieces of data. Let's look at the comparison operators available in Python.
==
This operator checks if the values on its left and right sides are equal. It is crucial to distinguish the comparison operator ==
(two equal signs) from the assignment operator =
(one equal sign), which is used to assign a value to a variable. Using a single =
where a comparison is intended is a common source of errors for new programmers.
# Comparing numbers
print(5 == 5)
print(10 == 7)
# Comparing strings
print("hello" == "hello")
print("Python" == "python") # Case matters!
# Assigning the result to a variable
are_numbers_equal = (100 == 100)
print(are_numbers_equal)
Running this code will output:
True
False
True
False
True
!=
This operator checks if the values on its left and right sides are not equal. It's the direct opposite of the ==
operator.
# Comparing numbers
print(5 != 5)
print(10 != 7)
# Comparing strings
print("hello" != "world")
print("Python" != "Python")
# Assigning the result
are_different = ("apple" != "orange")
print(are_different)
Output:
False
True
True
False
True
<
This operator checks if the value on the left side is strictly less than the value on the right side.
print(5 < 10)
print(10 < 5)
print(5 < 5) # Not less than, so False
# Strings are compared lexicographically (like in a dictionary)
print("apple" < "banana")
print("cat" < "car") # 't' comes after 'r'
Output:
True
False
False
True
False
>
This operator checks if the value on the left side is strictly greater than the value on the right side.
print(10 > 5)
print(5 > 10)
print(5 > 5) # Not greater than, so False
print("zebra" > "apple")
Output:
True
False
False
True
<=
This operator checks if the value on the left side is less than or equal to the value on the right side.
print(5 <= 10)
print(10 <= 5)
print(5 <= 5) # Equal to, so True
Output:
True
False
True
>=
This operator checks if the value on the left side is greater than or equal to the value on the right side.
print(10 >= 5)
print(5 >= 10)
print(5 >= 5) # Equal to, so True
Output:
True
False
True
Python is flexible when comparing different numeric types, like integers and floats. It generally converts them to a common type before comparing.
print(5 == 5.0)
print(10 > 9.99)
print(3 <= 3.0)
Output:
True
True
True
Here's a quick summary table:
Operator | Meaning | Example | Result |
---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 6 |
True |
< |
Less than | 5 < 10 |
True |
> |
Greater than | 10 > 5 |
True |
<= |
Less than or equal to | 5 <= 5 |
True |
>= |
Greater than or equal to | 10 >= 5 |
True |
Understanding these operators is essential because they form the basis of decision-making in programming. In the next chapter, "Controlling Program Flow," you'll see how the True
or False
results from these comparisons are used in if
statements and loops to direct how your program executes.
© 2025 ApX Machine Learning