Python Comparison Operator – PythonForBeginners.com


There are various types of operators like arithmetic operators, comparison operators, and bitwise operators in Python. In our programs, we use these operators to control the sequence of execution and to manipulate the data. In this article, we will study different python comparison operator , their functioning and examples.

What is a Python Comparison Operator?

Comparison operators are the binary operators used to compare two objects in a program. These are also called relational operators as they determine the relation between two objects in python.

A comparison operator returns a boolean value ‘True’ or ‘False’ after comparing the operands. 

Python equal to operator 

Python equal to operator is used to check if two objects are equal.

The syntax for equal to operator in python is a == b. Here a and b are the operands that are being checked for equality. The variables a and b can contain any object having primitive data types such as integer, float, or string or they may contain references to container objects like lists, tuples, sets and dictionaries. The output is True if both the operands are equal. Otherwise, the output will be False.

You can understand the working of equal to operator using the following program.

myNum1 = 14
myNum2 = 14
myNum3 = 10
myNum4 = 5
compare12 = myNum1 == myNum2
compare13 = myNum1 == myNum3
compare14 = myNum1 == myNum4
print("{} is equal to {}?: {}".format(myNum1, myNum2, compare12))
print("{} is equal to {}?: {}".format(myNum1, myNum3, compare13))
print("{} is equal to {}?: {}".format(myNum1, myNum4, compare14))

Output:

14 is equal to 14?: True
14 is equal to 10?: False
14 is equal to 5?: False

Python not equal to Operator 

Python not equal to operator is used to check if two objects are unequal.

The syntax for equal to operator in python is a != b. Here a and b are the operands that are being checked for inequality. The variables a and b can contain any object having primitive data types such as integer, float, or string or they may contain references to container objects like lists, tuples, sets and dictionaries. The output is False if both the operands are equal. Otherwise, the output will be True.

You can understand the working of not equal to operator using the following program.

myNum1 = 14
myNum2 = 14
myNum3 = 10
myNum4 = 5
compare12 = myNum1 != myNum2
compare13 = myNum1 != myNum3
compare14 = myNum1 != myNum4
print("{} is not equal to {}?: {}".format(myNum1, myNum2, compare12))
print("{} is not equal to {}?: {}".format(myNum1, myNum3, compare13))
print("{} is not equal to {}?: {}".format(myNum1, myNum4, compare14))

Output:

14 is not equal to 14?: False
14 is not equal to 10?: True
14 is not equal to 5?: True

Python Less than Operator 

Python less than operator is used to check if an object is less than another object.

The syntax for less than operator in python is a < b. The variables a and b can contain any object having primitive data types such as integer, float, or string or they may contain references to container objects like lists, tuples, sets and dictionaries. The output is True if a is less than b. Otherwise, the output will be False.

You can understand the working of the less than operator using the following program.

myNum1 = 14
myNum2 = 14
myNum3 = 10
myNum4 = 5
compare12 = myNum1 < myNum2
compare13 = myNum3 < myNum1
compare14 = myNum1 < myNum4
print("{} is less than {}?: {}".format(myNum1, myNum2, compare12))
print("{} is less than {}?: {}".format(myNum3, myNum1, compare13))
print("{} is less than {}?: {}".format(myNum1, myNum4, compare14))

Output:

14 is less than 14?: False
10 is less than 14?: True
14 is less than 5?: False

Less than operator does not support comparison between a string and a number data type such as float or int. If we try to perform such a comparison, TypeError will occur. You can avoid it using python try except blocks.

Python Greater than operator 

Python greater than operator is used to check if an object is greater than another object.

The syntax for greater than operator in python is a > b. The variables a and b can contain any object having primitive data types such as integer, float, or string or they may contain references to container objects like lists, tuples, sets and dictionaries. The output is True if a is greater than b. Otherwise, the output will be False.

You can understand the working of greater than operator using the following program.

myNum1 = 14
myNum2 = 14
myNum3 = 10
myNum4 = 5
compare12 = myNum1 > myNum2
compare13 = myNum1 > myNum3
compare14 = myNum1 > myNum4
print("{} is greater than {}?: {}".format(myNum1, myNum2, compare12))
print("{} is greater than {}?: {}".format(myNum1, myNum3, compare13))
print("{} is greater than {}?: {}".format(myNum1, myNum4, compare14))

Output:

14 is greater than 14?: False
14 is greater than 10?: True
14 is greater than 5?: True

Greater than operator does not support comparison between a string and a number data type such as float or int. If we try to perform such a comparison, TypeError will occur.

Python less than or equal to  

Python less than or equal to operator is used to check if an object is less or equal to another object.

The syntax for less than or equal to operator in python is a <= b. The variables a and b can contain any object having primitive data types such as integer, float, or string or they may contain references to container objects like lists, tuples, sets and dictionaries. The output is True if a is less or equal to b. Otherwise, the output will be False.

You can understand the working of less than or equal to operator using the following program.

myNum1 = 14
myNum2 = 14
myNum3 = 10
myNum4 = 5
compare12 = myNum1 <= myNum2
compare13 = myNum3 <= myNum1
compare14 = myNum1 <= myNum4
print("{} is less than or equal to {}?: {}".format(myNum1, myNum2, compare12))
print("{} is less than or equal to {}?: {}".format(myNum3, myNum1, compare13))
print("{} is less than or equal to {}?: {}".format(myNum1, myNum4, compare14))

Output:

14 is less than or equal to 14?: True
10 is less than or equal to 14?: True
14 is less than or equal to 5?: False

Python greater than or equal to

Python greater than or equal to operator is used to check if an object is greater or equal to another object.

The syntax for greater than or equal to operator in python is a >= b. The variables a and b can contain any object having primitive data types such as integer, float, or string or they may contain references to container objects like lists, tuples, sets and dictionaries. The output is True if a is greater than or equal to b. Otherwise, the output will be False.

You can understand the working of greater than or equal to operator using the following program.

myNum1 = 14
myNum2 = 14
myNum3 = 10
myNum4 = 5
compare12 = myNum1 >= myNum2
compare13 = myNum1 >= myNum3
compare14 = myNum1 >= myNum4
print("{} is greater than or equal to {}?: {}".format(myNum1, myNum2, compare12))
print("{} is greater than or equal to {}?: {}".format(myNum1, myNum3, compare13))
print("{} is greater than or equal to {}?: {}".format(myNum1, myNum4, compare14))

Output:

14 is greater than or equal to 14?: True
14 is greater than or equal to 10?: True
14 is greater than or equal to 5?: True

Conclusion

In this article, we have discussed every python comparison operator. We also discussed some examples and possible errors while performing comparisons with each python comparison operator. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here