Comparison operators
Contents
- 1. Comparison operators. List
- 2. Examples of program code that uses comparison operations
- 3. Compound comparison operations. Examples
- Related topics
Search other websites:
1. Comparison operators. List
To compare two values in Python, comparison operations are introduced. Comparison operations are binary, that is, they require two operands. The result of any comparison operation is the logical value True or False.
Most often, comparison operations occur in operators where the condition is checked (if, while) and the solution to the problem depends on the fulfillment or non-fulfillment of a certain condition. The following is a list of comparison operations in descending order of priority.
- ==, != – equality test operators (highest priority);
- <, >, <=, >= – comparison operators are correspondingly less, greater, less or equal, greater or equal.
⇑
2. Examples of program code that uses comparison operations
# comparison operators 5 < 6 # five less than six a = 8 b = 3 c = a == b # c = False c = a < b # c = False c = a!=b # c = True
⇑
3. Compound comparison operations. Examples
Python has the ability create a chain from multiple comparison operations. In this case, a chain of several operations of type
a < b < c
implicitly transforms the form in which each operation is presented in the usual way, but the operator and is used between adjacent operations
a < b and b < c
Example.
# Compound comparison operations a = 5 b = 7 c = a < b < 8 # c = True c = a != b != 8 # c = True c = a == b == 8 # c = False
⇑
Related topics
- Operators (operations) for working with numerical objects. Operator priority table
- Mixing types. Type conversion in operators. The rules
- Mathematical (arithmetic) operators. Examples
- Bitwise operators
⇑