Python Comparison Operators https://www.skillvertex.com/blog Thu, 11 Apr 2024 12:00:25 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Python Comparison Operators https://www.skillvertex.com/blog 32 32 Python Comparison Operators https://www.skillvertex.com/blog/python-comparison-operators/ https://www.skillvertex.com/blog/python-comparison-operators/#respond Thu, 11 Apr 2024 12:00:25 +0000 https://www.skillvertex.com/blog/?p=6966 Read more]]>

Table of Contents

Python comparison operators will compare two values. == or equal to, != or not equal to, > or greater than, >= or greater than or equal to, < or less than, and <= or less than or equal to are the six Python comparison operators. Read this article to learn more about Python Comparison Operators.

What is a Python Comparison Operator

Comparison Operators in Python play an essential role in Python’s conditional statements (if, else, and elif) and looping statements (while loops). These comparison operators are also known as relational operators. Hence, the known operators will stands for < and > will be for greater operators.

Furthermore, python will use two operators by adding the = symbol with these two. The <= symbol will be less than or equal to the operator and these >= symbols will stand for greater than or equal to the operator.

It is known that Python has two operators that are ”==” and ” !=”. The <= symbol will stand for less than or equal to the operator, whereas the ”>=” is referred to as the greater than or equal to the operator.

Look at the table below to learn more about six comparison operators in Python.

<Less thana<b
>Greater thana>b
<=Less than or equal toa<=b
>=Greater than or equal toa>=b
==Is equal toa==b
!=Is not equal toa!=b

Comparison operators are mostly binary and need two operands. An expression that has a comparison operator is known as a Boolean expression. This will either come as true or false.

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

Output

False
True

Both the operands can either be Python literals, variables or expressions. As Python has mixed arithmetic it is possible to have any number-type operands.

The code provided below will show the use of Python comparison operators with integer numbers

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)

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

Comparison of Float number

This example will show the comparison of an integer and float operand.

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)

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

What is the Comparison of Complex numbers?

A complex object is considered as the number data type in Python. It will work differently from others in behavior. Python won’t support < and> operators as it cannot 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)

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

Here, it is possible to get a type error that is less than or greater than the 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)

Output

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

Comparison of Booleans

Boolean objects are referred to as real integers such as true is 1 and false is 0. However, python will consider any non-zero numbers as true=. Hence, it is allowed to compare the boolean objects. 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)

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

What is the Comparison of Sequence Types?

The comparison of one similar sequence object will be operated in Python. A string object can be compared with another string only. A list can’t be compared with a tuple even if it holds the same items.

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

Output

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

From the example provided above, it is understood that the sequence objects will be compared with the help of a lexicographical ordering mechanism. This comparison will begin with the item at the 0th index. Hence, if they come as equal, this comparison will move to the next index until they get the items at the certain index unequal or the sequence is exhausted.

However, if one sequence is considered as the initial sub-sequence of the other, then the shorter sequence will be the lesser one.

The operators will be considered greater and that will depend on the difference in the values of items when they are not equal. An example of this is BAT’>’BAR’ is True, if the T comes after R in Unicode order.

All the items in the two sequences will be compared for equity. Thus, the sequence will be equal while comparison.

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)

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

Another example given below illustrates the two tuple objects.

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)

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

What is the Comparison of Dictionary objects?

While comparing the dictionary objects, “< ” and “>” operators are used in the Python dictionary and they are not defined. Whereas, in operands, this type error “<” cannot be supported in between the instances of ‘dict’ and ‘dict’ will be reported.

Whereas. the equality comparison will monitor if the length of both the dict items is the same. The length of the dictionary will be considered as the number of key-value pairs in it.

However, python dictionaries will be compared with the length. The dictionaries that have fewer elements won’t be referred to as dictionaries. Since dictionaries require 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)

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

Conclusion

Comparison Operators in Python is a relevant topic for beginners to learn. Learning these operators will be useful during coding and writing Python.

Python Comparison Operators- FAQs

Q1. What are the 6 comparison operators in Python?

Ans == or equal to, != or not equal to, > or greater than, >= or greater than or equal to, < or less than, and <= or less than or equal to

Q2. What is Elif in Python?

Ans. Elif will stand for if else and it functions to test the multiple conditions in Python.

Q3. What is float in Python?

Ans. Float is referred to as the reusable code in Python that can turn values into floating point numbers.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/python-comparison-operators/feed/ 0