Python Tutorial - Comparison Operators

Python Exercise Lists

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

The difference between == and is in Python

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)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises