VMware ESXi and vSphere Cluster Management
Python Comparison Operators
Learn how Python comparison operators such as ==, !=, >, <, >=, and <= test values and return True or False.
What comparison operators do
A comparison operator tests the relationship between two values. Each value or expression being compared is an operand. A comparison has this general structure:
left operand operator right operandFor example:
3 < 5Here, 3 is the left operand, < is the comparison operator, and 5 is the right operand. Python evaluates the complete comparison to a Boolean result: True or False.
A Boolean is a value with one of two possibilities: True or False. A comparison is an expression because Python evaluates it to produce a value.
Python comparison operators
| Operator | Meaning | Example expression | Example Boolean result |
|---|---|---|---|
== | Equal value | 4 == 4 | True |
!= | Not equal; values differ | 4 != 7 | True |
> | Greater than | 8 > 3 | True |
< | Less than | 3 < 8 | True |
>= | Greater than or equal to | 5 >= 5 | True |
<= | Less than or equal to | 5 <= 5 | True |
Equality with ==
The equality operator, ==, tests whether two values have the same value. It does not change either value.
10 == 10 # True
10 == 12 # False== is a test. Do not confuse it with =, which assigns a value to a variable:
score = 10 # assignment
score == 10 # equality testInequality with !=
The inequality operator, !=, tests whether two values differ.
10 != 12 # True
10 != 10 # FalseThe result is True when the operands have different values and False when they are equal.
Greater-than and less-than comparisons
The > and < operators make directional comparisons, commonly between numbers.
3 > 1 # True: 3 is greater than 1
1 > 2 # False: 1 is not greater than 2
1 < 2 # True: 1 is less than 2
3 < 2 # False: 3 is not less than 2These operators do not include equality. For example, 5 > 5 and 5 < 5 are both False.
Inclusive comparisons with >= and <=
The greater-than-or-equal operator, >=, is True when the left operand is greater than or equal to the right operand. The less-than-or-equal operator, <=, is True when the left operand is less than or equal to the right operand.
7 >= 5 # True: 7 is greater
5 >= 5 # True: the values are equal
3 >= 5 # False
3 <= 5 # True: 3 is less
5 <= 5 # True: the values are equal
7 <= 5 # FalseThe equal case is the important difference between > and >=, and between < and <=. Inclusive comparisons are useful for boundaries such as a minimum or maximum allowed value.
Comparing values stored in variables
Variables can hold the operands of a comparison. The comparison then evaluates the current values stored in those variables.
x = 5
y = 7
x == y # False
x != y # True
x > y # False
x < y # True
x >= y # False
x <= y # TrueBecause x contains 5 and y contains 7, the values are different, x is less than y, and x is not greater than or equal to y.
Interactive-shell demonstration
You can enter the following in a Python interactive shell. The shell displays the Boolean result of each expression:
>>> x = 5
>>> y = 7
>>> x == y
False
>>> x != y
True
>>> x > y
False
>>> x < y
True
>>> x >= y
False
>>> x <= y
TrueComparisons involving arithmetic expressions
A comparison can contain arithmetic expressions. Python evaluates the arithmetic and then compares the resulting values. For example, with x = 5 and y = 7, the sum x + y is 12.
x = 5
y = 7
x + y == 12 # True
x + y < 15 # True
x + y > 20 # False
x + y <= 12 # TrueArithmetic expressions can appear on either side of a comparison:
x + y == 6 + 6 # True
20 > x + y # TrueIn an expression such as x + y <= 12, Python evaluates x + y first and then applies the comparison. This ordering is an example of operator precedence, the order Python uses when evaluating operators. Parentheses can make the intended order explicit:
(x + y) <= 12Using comparison results in conditional logic
Comparison results are commonly used as conditions in control flow, such as an if statement:
temperature = 20
if temperature >= 18:
print("Comfortable")The indented code runs because temperature >= 18 evaluates to True. Comparisons can also be combined with Python's logical operators, such as and, or, and not:
age = 25
has_ticket = True
if age >= 18 and has_ticket:
print("Entry allowed")Logical operators are useful when a decision depends on multiple Boolean comparisons.
Common mistakes and troubleshooting
- Using
=to compare: use==for an equality test. The single equals sign assigns a value, as incount = 3. - Expecting an original value: a standard comparison returns
TrueorFalse, not one of the operands. - Misreading inclusive operators:
>=and<=include the equal case. Thus,5 >= 5and5 <= 5are bothTrue. - Ignoring arithmetic: calculate the arithmetic portion before interpreting the comparison. With
x = 5andy = 7,x + y <= 12means12 <= 12, which isTrue.
Summary
- A comparison operator tests the relationship between two operands.
- Every comparison evaluates to the Boolean value
TrueorFalse. ==tests equality, while!=tests inequality.>and<test strict ordering.>=and<=test ordering while including equality.- Variables and arithmetic expressions can be used as operands.
- Comparison results commonly provide conditions for
ifstatements and can be combined with logical operators.
For a related reference, see Comparison Operators.