Comparison Operators¶
Provide provides many comparison operators for checking to see if certain relationships exist between two values. Each comparison operator will evaluate as single Boolean value. The most commonly used comparison operators are:
Operator |
Meaning |
---|---|
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
=> |
Greater than or equal to |
== |
Equal to |
!= |
Not equal to |
Note that the “equal to” comparison operator (==
) consists of two equals signs, while the assignment operator (=
) consists of one equal sign. Mixing up these operators is a very common mistake, even for experienced programmers.
When one of the comparison operators appears between two values, Python will perform the indicated comparison, which will evaluate to either True
or False
.
Numerical Comparisons¶
Comparison operators are most commonly used to compare numerical values. When used between two numbers, as comparison operator will have the same meaning as the associator comparison from mathematics. Let’s see some examples.
# Less than
2 < 6
True
# Greater than
2 > 9
False
# Equality operator
1 == 7
False
6 == 2*3
True
# Not equal
6 != 10 - 3
True
All of the comparison examples above involved comparisons between integers. It is worth mentioning that Python will compare integers and floats without complaint.
a = 17
b = 17.0
print(type(a))
print(type(b))
<class 'int'>
<class 'float'>
a == b
True
String Comparisons¶
We can also perform comparisons between strings. In this case, a Python performs a lexicographical comparison in which strings are ordered alphabetically, with letters appearing earlier in the alphabet being “less than” those later in the alphabet. The comparison is case sensitive, with uppercase letters appearing before lowercase letters in the ordering.
'zebra' > 'aardvark'
True
'aardvark' < 'ant'
True
'aardvark' < 'Bat'
False
'Ant' == 'ant'
False
If you ever wish to compare two strings in a case-insensitive way, then you can first convert both strings to lowercase using the lower()
string method.
s1 = 'Zebra'
s2 = 'zebra'
s1.lower() == s2.lower()
True
Python has a few special comparison operators that can be used to detect specific types of relationships between strings. For example, strings can be compared using the in
and not in
operators.
The
in
operator will returnTrue
if the string on the left side of the operator appears within the string on the right side, andFalse
otherwise.The
not in
operator will returnTrue
if the string on the left of the operator DOES NOT appear within the string on the right side, andFalse
otherwise.
my_string = 'This land is my land.'
print('land' in my_string)
print('island' in my_string)
print('Land' not in my_string)
True
False
True
Comparings Strings and Integers¶
In some situations, it is valid to use the comparison operators between values of different data types. Although (with the notable exception of comparisons between int
and float
values) the results are often not that interesting.
For example, you can use !=
and ==
to compare a string with a numerical value, but Python will always interpret the two items as being unequal, regardless of the values.
str7 = "7"
int7 = 7
str7 == int7
False
Attempting to compare a strings with a number using <
, >
, <=
, or >=
will produce an error.
str7 < int7
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-d1a71b3d6a42> in <module>
----> 1 str7 < int7
TypeError: '<' not supported between instances of 'str' and 'int'