2.5. Section: Comparison Operators in Python#
In any programming language, including Python, comparison operators are crucial for making decisions and evaluating conditions. They allow you to compare two values and determine the relationship between them, such as whether one is greater than, less than, or equal to the other. The result of a comparison is always a boolean value: either True
or False
. This makes comparison operators particularly useful in control flow statements like if
, while
, and for
loops, where decisions are made based on specific conditions.
2.5.1. What are Comparison Operators?#
Comparison operators, also known as relational operators, are symbols or keywords that allow you to compare two values. These comparisons can involve numbers, strings, or other comparable data types, and they return a True or False result depending on whether the comparison holds.
5 > 3 # Evaluates to True because 5 is greater than 3
True
5 < 3 # Evaluates to False because 5 is not less than 3
False
2.5.2. Python Comparison Operators#
Comparison operators are used to compare two values. The result of these comparisons is always a boolean (True
or False
). These operators are crucial in decision-making statements like if
and while
, helping to control the flow of your program.
Operator |
Description |
True Example |
Result |
False Example |
Result |
---|---|---|---|---|---|
|
Equal to |
|
|
|
|
|
Not equal to |
|
|
|
|
|
Greater than |
|
|
|
|
|
Less than |
|
|
|
|
|
Greater than or equal to |
|
|
|
|
|
Less than or equal to |
|
|
|
|
2.5.3. Multiple comparison operators#
In Python, you can chain multiple comparison operators together to create more complex conditions in a single line. Python evaluates the conditions from left to right, and the final result is True
only if all conditions in the chain are True
.
x = 10
print(5 < x < 15) # True, because x (10) is greater than 5 and less than 15
True
In the above example, the expression 5 < x < 15
is equivalent to checking:
5 < x and x < 15
True
Both conditions (5 < x and x < 15
) must be True
for the entire expression to evaluate as True.
You can also chain other comparison operators:
y = 8
print(3 < y <= 8) # True, because y is greater than 3 and less than or equal to 8
True
2.5.4. String Comparisons#
Python allows you to use comparison operators to compare strings as well. These comparisons are done lexicographically, which means Python compares strings character by character using the Unicode (ASCII) values of each character.
2.5.4.1. String Comparison Rules:#
Alphabetical Order: Characters are compared based on their position in the alphabet. Uppercase letters come before lowercase letters in the Unicode ordering, so “A” is considered less than “a”.
Length: If the strings are of different lengths but are otherwise the same, the shorter string is considered smaller.
print("apple" < "banana") # True, because "apple" comes before "banana" alphabetically
print("apple" == "Apple") # False, because Python is case-sensitive
print("cat" > "bat") # True, because "c" comes after "b" alphabetically
True
False
True
In these examples, Python is comparing the Unicode values of each character in the strings:
In these examples, Python is comparing the Unicode values of each character in the strings:
"apple"
<"banana"
: Since"a"
comes before"b"
alphabetically, the expression evaluates toTrue
."apple" == "Apple"
: This returnsFalse
because Python is case-sensitive, and uppercase"A"
has a lower Unicode value than lowercase"a"
."cat" > "bat"
: This returnsTrue
because the first letter"c"
is greater than"b"
in lexicographical order.
2.5.4.2. Example: Comparing Strings of Different Lengths#
print("abc" < "abcd") # True, because "abc" is shorter and identical to the first part of "abcd"
True
In this case, Python compares the strings character by character. Once the characters in “abc” match all the characters in the same positions in “abcd”, Python considers the shorter string to be “less than” the longer one.