Comparison operators, also known as relational operators, are used to compare the results of constants, variables, or expressions. If this comparison is true, returns True, otherwise returns False.
The comparison operators supported by Python are shown in the following table.
Comparison Operators | Comparison Operator Name | Explanation |
---|---|---|
> | Greater than | If the value before > is greater than the value after, then return True, otherwise return False. |
< | Less than | If the value before < is less than the value after it, return True, otherwise return False. |
== | Equal | If the values on both sides of == are equal, returns True, otherwise returns False. |
>= | Greater than or equal | Equivalent to ≥ in mathematics, if the value before > = is greater than or equal to the value after, return True, otherwise return False. |
<= | Less than or equal | Equivalent to ≤ in mathematics, if the value before <= is less than or equal to the value after, return True, otherwise return False. |
!= | Not equal | Equivalent to ≠ in mathematics, returns true if the values on both sides of ! = are not equal, otherwise returns false. |
is | - | Determines whether the objects referenced by the two variables are the same, returns True if they are the same, otherwise returns False. |
is not | - | Determine whether the objects referenced by the two variables are different. If they are not the same, return True, otherwise return False. |
For example:
print ("78 greater than 101:", 78> 101) print ("23 * 6 greater than or equal to 56:", 23 * 6> = 56) print ("83.5 equal to 83.5:", 83.5 == 83.5) print ("25 equal to 25.0:", 25 == 25.0) print ("False less than True:", False < True) print ("True equal to True:", True == True)
The output is:
78 greater than 101: False
23 * 6 greater than or equal to 56: True
83.5 equal to 83.5: True
25 equal to 25.0: True
False less than True: True
True equal to True: True
Beginners of Python may be unfamiliar with is, so many people mistake it for the function of ==, but in fact, is and == are fundamentally different and are not the same thing.
== is used to compare the values of two variables, and is is used to compare whether the two variables refer to the same object, for example:
import time #Import time module t_1 = time.gmtime () # gmtime () is used to get the current time t_2 = time.gmtime () print (t_1 == t_2) print (t_1 is t_2)
The output is:
True
False
The gmtime () method from the time module is used to get the current system time, accuracy up to second level. Because the program runs very fast, t_1 and t_1 get the same time. == is used to determine whether the values of t_1 and t_2 are equal, so it returns True.
Although the values of t_1 and t_2 are equal, they are two different objects (each call to gmtime () returns a different object), so t_1 is t_2 returns False. It's like two twin sisters. Although they look the same, they are two people.
So how do you tell if two objects are the same? The answer is to determine the memory addresses of the two objects. If the memory addresses are the same, it means that the two objects use the same memory, which is of course the same object; this is like two names using the same body, of course, the same person.
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises