Operator | Description |
---|---|
== | Equal to |
!= | Not Equal |
> | Greater than |
< | Less than |
>= | Less than or Equal to |
<= | Less than or Equal to |
is | Object identity/reference comparison |
Notes
All comparison operators have the same priority.
The priority of comparison operators is lower than the mathematical operators.
Let’s check the evaluation and usage of the operators-
Equal to Comparison
print(10 == 10)
print("big box code" == "big box code")
print(10 == 200)
print("big box code" == "Big Box Code")
print(10 == 10.00)
PythonOutput:
True
True
False
False
True
PlaintextNot Equal to Comparison
print(10 != 10)
print(10 != 10.00)
print(10 != 200)
print(200 != 10)
print("big box code" != "big box code")
print("big box code" != "Big Box Code")
print("Big Box Code" != "big box code")
PythonOutput:
False
False
True
True
False
True
True
PlaintextGreater than Comparison
print(10 > 10)
print(10 > 10.00)
print(10 > 200)
print(200 > 10)
print("big box code" > "big box code")
print("big box code" > "Big Box Code")
print("Big Box Code" > "big box code")
PythonOutput:
False
False
False
True
False
True
False
PlaintextLess than Comparison
print(10 < 10)
print(10 < 10.00)
print(10 < 200)
print(200 < 10)
print("big box code" < "big box code")
print("big box code" < "Big Box Code")
print("Big Box Code" < "big box code")
PythonOutput:
False
False
True
False
False
False
True
PlaintextGreater than or Equal to Comparison
print(10 >= 10)
print(10 >= 10.00)
print(10 >= 200)
print(200 >= 10)
print("big box code" >= "big box code")
print("big box code" >= "Big Box Code")
print("Big Box Code" >= "big box code")
PythonOutput:
True
True
False
True
True
True
False
PlaintextLess than or Equal to Comparison
print(10 <= 10)
print(10 <= 10.00)
print(10 <= 200)
print(200 <= 10)
print("big box code" <= "big box code")
print("big box code" <= "Big Box Code")
print("Big Box Code" <= "big box code")
PythonOutput:
True
True
True
False
True
False
True
PlaintextChaining Comparison Operators
We can chain the use of comparison operators, and those will be evaluated as separate conditions with “and” checking-
x = 10
y = 20
print(10 == x == 10) # equivalent to (10 == x and x == 10)
print(y == x == 10) # equivalent to (y == x and x == 10)
print(y > x == 10) # equivalent to (y > x and x == 10)
PythonOutput:
The expressions are evaluated as below
Expression | Equivalent Expression |
---|---|
10 == x == 10 | 10 == x and x == 10 |
y == x == 10 | y == x and x == 10 |
y > x == 10 | y > x and x == 10 |
Here are the result of the above code-
True
False
True
PlaintextCheck some more examples-
x = 5
y = 10
z = 20
print(x < y < z) # equivalent to (x < y and y < z)
print(x > y <= z) # equivalent to (x > y and y <= z)
PythonOutput:
The evaluation will be done as below-
Expression | Equivalent Expression |
---|---|
x < y < z | x < y and y < z |
x > y <= z | x > y and y <= z |
Here is the output-
True
False
PlaintextObject Reference Comparison – is
“is” operator is used to check if 2 variables refer to the same object or not. If 2 variables refer to the same object the “is” will return True.
Compare Primitive Data
Let’s start with the primitive data types. We are defining here 2 integers with the same value and then checking if those 2 variables refer to the same object or not-
first_product_price = 100
second_product_price = 100
third_product_price = 200
print('First Compare result: ', first_product_price is second_product_price)
print('Last Compare result: ', first_product_price is third_product_price)
print(f'first product price object id: {id(first_product_price)}')
print(f'second product price object id: {id(second_product_price)}')
print(f'third product price object id: {id(third_product_price)}')
PythonOutput:
We can see “first_product_price” and “second_product_price” has the same value, so internally those to variables refer to the same integer object. We can check the id of those variables and get the same id.
And the “third_product_price” has some different value, so it refers to some different object.
First Compare result: True
Last Compare result: False
first product price object id: 140135950814544
second product price object id: 140135950814544
third product price object id: 140135950817744
PlaintextSame is true for other primitive data types – string, float etc.
Compare None
We get the same result if None is assigned to a variable-
current_state = None
future_state = None
print(current_state is None)
print(future_state is None)
print(current_state is future_state)
PythonOutput:
True
True
True
PlaintextCompare List (dictionary and set)
Now let’s check the same thing with composite data types like list then we get a different result. This happens for the directory and set.
first_list = [100, 200, 900]
second_list = [100, 200, 900]
third_list = first_list
print('first_list is second_list check: ', first_list is second_list)
print('first_list is thid_list check: ', third_list is first_list)
PythonOutput:
first_list and second_list have the same set of value, but those are 2 different objects. So “is” returns False when first_list and second_list is compared.
first_list is second_list check: False
first_list is thid_list check: True
PlaintextCompare Tuple
2 Tuples that have the same values in the same sequence then those Tuples refer to the same object. If the sequence changes then it is a different object.
first_tpl = (100, 200, 900)
second_tpl = (100, 200, 900)
third_tpl = first_tpl
another_tpl = (200, 100, 900)
print('first_tpl is second_tpl check: ', first_tpl is second_tpl)
print('first_tpl is thid_tpl check: ', third_tpl is first_tpl)
print('first_tpl is another_tpl check: ', third_tpl is another_tpl)
print('first_tpl id: ', id(first_tpl))
print('second_tpl id: ', id(second_tpl))
print('third_tpl id: ', id(third_tpl))
print('another_tpl id: ', id(another_tpl))
PythonOutput:
first_tpl and second_tpl have the same values in the same sequence, so those variables refer to the same object.
But another_tpl has a different sequence of values, though it has the same set of values. So it refers to different objects.
first_tpl is second_tpl check: True
first_tpl is thid_tpl check: True
first_tpl is another_tpl check: False
first_tpl id: 140288779343872
second_tpl id: 140288779343872
third_tpl id: 140288779343872
another_tpl id: 140288779124800
Plaintext