Python comparison operators

Python Comparison Operators

Comparison operators in Python are crucial in conditional statements (if, else, and elif) and loops (while and for loops). Like arithmetic operators, the comparison operator “-” (also known as the relational operator, with < representing less than and > representing greater than) is well known.

Python also uses two operators that are combined with the “=” symbol. The “<=" symbol represents less than or equal to. The ">=” symbol represents greater than or equal to.

Python also has two comparison operators, == and !=. These are used for equality and inequality. Therefore, there are six comparison operators in Python.

< less than a<b
> Greater than a>b
<= Less than or equal a<=b
>= Greater Than or Equal to a>=b
== Equal to a==b
!= Not Equal to a!=b

Comparison operators are binary and require two operands. Expressions involving comparison operators are called Boolean expressions and always return True or False.

a=5
b=7
print (a>b)
print (a<b)

It will produce the following output: Output

False
True

The two operands can be Python literals, variables, or expressions. Because Python supports mixed arithmetic, you can use operands of any numeric type.

The following code demonstrates the use of Python’s comparison operators with integers.

print ("Both operands are integer")
a=5
b=7
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

It will produce the following output

Output

Both operands are integer
a= 5 b= 7 a>b is False
a= 5 b= 7 a<b is True
a= 5 b= 7 a==b is False
a=5 b = 7 a!=b is True

Floating-Point Comparison

In the following example, an integer and a floating-point operand are compared.

print ("comparison of int and float")
a=10
b=10.0
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

It will produce the following output

Output

comparison of int and float
a= 10 b= 10.0 a>b is False
a= 10 b= 10.0 a<b is False
a=10 b = 10.0 a==b is True
a = 10 b = 10.0 a!=b is False

Comparison of Complex Numbers

Although complex numbers are considered a numeric data type in Python, they behave differently from other types. Python does not support the < and > operators, but does support the equality (==) and inequality (!=) operators.

print ("comparison of complex numbers")
a=10+1j
b=10.-1j
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

This will produce the following output −

comparison of complex numbers
a= (10+1j) b= (10-1j) a==b is False
a= (10+1j) b= (10-1j) a!=b is True

You encountered a TypeError when using the less than or greater than operator.

print ("comparison of complex numbers")
a=10+1j
b=10.-1j
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)

It will produce the following output

comparison of complex numbers
Traceback (most recent call last):
  File "C:Usersmlathexamplesexample.py", line 5, in <module>
    print ("a=",a, "b=",b,"a<b is",a<b)
                                      ^^^
TypeError: '<' not supported between instances of 'complex' and
'complex

Boolean Comparisons

In Python, Boolean objects are actually integers: True is 1, False is 0. In fact, Python considers any non-zero number to be True. Comparisons of Boolean objects are possible in Python. “False < True" is True!

print ("comparison of Booleans")
a=True
b=False
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

It will produce the following output

comparison of Booleans
a= True b= False a<b is False
a= True b= False a>b is True
a= True b= False a==b is False
a= True b= False a!=b is True

Sequence Type Comparison

In Python, only sequence objects of the same type can be compared. String objects can only be compared with other string objects. Lists and tuples cannot be compared, even if they have the same elements.

print ("comparison of different sequence types")
a=(1,2,3)
b=[1,2,3]
print ("a=",a, "b=",b,"a<b is",a<b)

This will produce the following output −

comparison of different sequence types
Traceback (most recent call last):
File "C:Usersmlathexamplesexample.py", line 5, in <module>
print ("a=",a, "b=",b,"a<b is",a<b)
^^^
TypeError: '<' not supported between instances of 'tuple' and 'list'

Sequence objects are compared using a lexicographical comparison mechanism. Comparison starts at the item indexed at 0. If they are equal, the comparison moves to the next index until the items at some index are unequal, or one of the sequences is exhausted. If one sequence is the initial subsequence of the other, the shorter sequence is considered smaller.

Which of the operands is greater depends on the difference in the values of the items at the unequal indices. For example, ‘BAT’ > ‘BAR’ is True because T comes after R in Unicode sorting.

Two sequences are considered equal if all their items are equal.

print ("comparison of strings")
a='BAT'
b='BALL'
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

It will generate the following output

comparison of strings
a= BAT b= BALL a<b is False
a= BAT b= BALL a>b is True
a= BAT b= BALL a==b is False
a= BAT b= BALL a!=b is True

In the following example, two tuple objects are compared –

print ("comparison of tuples")
a=(1,2,4)
b=(1,2,3)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

It will produce the following output −

a= (1, 2, 4) b= (1, 2, 3) a<b is False
a= (1, 2, 4) b= (1, 2, 3) a>b is True
a= (1, 2, 4) b= (1, 2, 3) a==b is False
a= (1, 2, 4) b= (1, 2, 3) a!=b is True

Comparison of Dictionary Objects

In Python, the “<" and ">” operators are undefined for dictionaries. When using these operators, a TypeError: ‘<' is not supported between instances of 'dict' and 'dict' is reported.

Equality comparisons check whether dictionary items have the same length. The length of a dictionary is the number of key-value pairs in it.

Python dictionaries are compared only by length. A dictionary with fewer elements is considered smaller than a dictionary with more elements.

print ("comparison of dictionary objects")
a={1:1,2:2}
b={2:2, 1:1, 3:3}
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

It will produce the following output:

Output

comparison of dictionary objects
a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a==b is False
a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a!=b is True

Leave a Reply

Your email address will not be published. Required fields are marked *