Python Comparison Operators are used to compare values and determine whether one value is greater than, less than, equal to, or not equal to another value.
Comparison Operators
Here are the most commonly used Python Comparison Operators:
==
(Equal to): It returns True if the values on either side of the operator are equal. For example,5 == 5
will returnTrue
.!=
(Not equal to): It returns True if the values on either side of the operator are not equal. For example,5 != 4
will returnTrue
.>
(Greater than): It returns True if the value on the left side of the operator is greater than the value on the right side. For example,5 > 4
will returnTrue
.<
(Less than): It returns True if the value on the left side of the operator is less than the value on the right side. For example,4 < 5
will returnTrue
.>=
(Greater than or equal to): It returns True if the value on the left side of the operator is greater than or equal to the value on the right side. For example,5 >= 5
will returnTrue
.<=
(Less than or equal to): It returns True if the value on the left side of the operator is less than or equal to the value on the right side. For example,4 <= 5
will returnTrue
.
Example
Python
x = 5
y = 4
# Equal to
print(x == y) #False
# Not equal to
print(x != y) #True
# Greater than
print(x > y) #True
# Less than
print(x < y) #False
# Greater than or equal to
print(x >= y) #True
# Less than or equal to
print(x <= y) #False
You can also use these comparison operators with variables and other data types like strings and lists.
Note: Python Comparison Operators are case-sensitive. For example, "Hello" != "hello"
will return True
.