Here we are going to see about comparators in python. We are going to learn about all the six python comparators and how to use them. And we are going to know about how to compare lists and strings.
Comparators are used to take two values and compare them together. It compares and produces the boolean values. It is commonly used with numerical values, but it can be used with other data types as well so.
What are the Comparators in Python?
The Python Comparators commonly take two values and compares them. It normally produces boolean values as True or False. For example, if we give 3>2, it produces the result as True. We have six comparators in Python. They are,
Python Comparators | Symbols |
---|---|
greater than | > |
greater than or equal to | >= |
less than | < |
less than or equal to | <= |
equal to | == |
not equal to | != |
Greater than operator
> is a symbol used for greater-than operator. It compares the left side value and the right side value. If the left-side value is greater, it returns True. Otherwise, it returns False.
Example 1
>>> 3 > 2
True
Here it returns True because three is greater than 2.
Example 2
>>> "Python">"python"
False
Here it checks based on the ASCII value. The ASCII value for P is 80 and for p is 112. So the ASCII value of “p” is greater than “P”; hence it returns False.
Example 3
>>> len("return")>len("ad")
True
Now we are going to compare based on the length of the string. For that, we are using the len() function. It returns True because the length of the return is greater.
Greater than or equal to operator
>= is a symbol used for greater than or equal to operator. It compares the left side value and the right side value. If the left-side value is greater than or equal, it returns True. Otherwise, it returns False.
Example
>>> 3<=4
True
>>> 4>=4
True
In the first statement, it returns True because 4 is greater than 3. The other statement also returns True because 4 is equal to 4.
Less than operator
< is a symbol used for the less-than operator. It compares the left value and right value. If the left-side value is lesser, it returns True. Otherwise, it returns False.
Example
>>> 4<7
True
4 is less than 7, so the result is True.
Less than or equal to operator
<= is a symbol used for less than or equal to operator. It compares the left side value and the right side value. If the left-side value is lesser or equal, it returns True. Otherwise, it returns False.
Example
>>> 4<6
True
>>> 9<=9
True
4 is less than 6, so the result is True, and 9 is equal to 9, so it returns True.
Equal to operator
== is a symbol used for equal to operator. It compares the left value and right value. If the left-side value is equal to the right-side value, it returns True. Otherwise, it returns False.
Example
>>> 6==0
False
6 is not equal to 0, so it returns False
Not equal to operator
!= is a symbol used for not equal to operator. It compares the left value and right value. If the left-side value is not equal to the right-side value, it returns True. Otherwise, it returns False.
Example
>>> 4!=4
False
4 is equal to 4, but it is given as not equal to, so it returns False.
Comparing a list
Until we saw about all the comparators available in python, now using those comparators, we will compare the list.
Code
lst1=[2,5,6]
lst2=[4,2,9]
if lst1>lst2:
print("list1 is greater than list2")
elif lst1<lst2:
print("list2 is greater than list1")
else:
print("both the list are equal")
First, we are giving two lists to compare. And then using if-else statements to compare the lists. The size of list1 and list2 is the same, but we are giving different elements in both lists. The elements in list2 are greater than the elements in list1. So it returns the result of list2 is greater than list1.
Output
Enter size of list:2 Enter any number:4 Enter any number:7 Enter size of list:2 Enter any number:9 Enter any number:7 list2 is greater than list1
Must Read | How to Remove Quotes From a String in Python
Comparing a string using python comparators
Now we are using the comparators to compare the length of the strings.
Code
str1="Python"
str2="Programming"
if len(str1)>len(str2):
print("The length of the ",str1,"is greater than the length of",str2)
elif len(str1)<len(str2):
print("The length of the ",str2,"is greater than the length of",str1)
else:
print("Both the strings are in same length")
Here we are comparing the strings based on their length. For that, we are using the len() function. The length of string2 is greater than string2. So it returns Programming is greater than Python.
Output
The length of the Programming is greater than the length of Python.
How to overload these comparators in classes?
One operator that performs several tasks is known as overloading. For example, if we take, + operator is used for several purposes like the addition of two integers, concatenation, and merging two lists.
Code
class comparators:
def __init__(self, a):
self.a = a
def __gt__(self, other):
if(self.a>other.a):
return True
else:
return False
obj1 = comparators(10)
obj2 = comparators(53)
if(obj1>obj2):
print("obj1 is greater than obj2")
else:
print("obj2 is greater than obj1")
First, we created a class named “comparators”. And then created objects. In this, we have given obj1=comparators(10) and obj2=comparators(53). It compares both the object and returns, which is greater.
Output
obj2 is greater than obj1
Frequently asked questions related to Comparators
Six types of comparators are available in Python.
Comparators are used to take two values and compare them together. It compares and produces the boolean values. It is commonly used with numerical values, but it can be used with other data types as well so.
Yes, the comparator returns boolean values as True or False.
len() function is used to compare a string.
greater than, greater than or equal to, less than, less than or equal to, equal to, not equal to are the types of comparators.
Conclusion
Here we have seen some comparators available in python. Totally we have six comparators in python. The above shown are just an example we can do a lot of things using comparators.