What are the Python comparison operators used for?
Comparison operators also known as relational operators are used to comparing values of two operands. Depending on the condition being tested, comparison operators may return True or False.
Python comparison operators can be used together with the if statement to form different logic to compare different values. For example, we can use comparison operators to determine if one value is equal to, greater than, less than or not equal to another value.
Python has six types of comparison operators as we would see shortly that can be used to compare both numeric types and strings as well. These operators include the greater than, less than, greater than or equal to, less than or equal to, not equal to and equal to operators.
Equal to Operator (==)
The Python equal to operator is used to determine if values on both sides of the operator are equal. The operator returns True if the values are equal and False if that is not the case.
In the short example below the operator returns False since the variables are not equal.
Not Equal to Operator(!=)
The not equal to Python comparison operator returns True if the values are not equal to one another and False if the values are equal to one another. Using variables from our earlier example the not equal to operator should return True since the values are indeed not equal to one another.
Greater than Operator (>)
The greater than operator allows you to check if the value on the left is greater than that on the right side of the operator. If the value of the left side is greater than that on the right, a True value is returned otherwise a False value is returned.
Less than operator (<)
The less-than Python comparison operator checks if the value of the left side of this operator is less than on the right side. If so a True value is returned and if otherwise a False value is printed.
In the example below x is not less than y therefore a False boolean value is returned.
The Greater than or Equal to Operator (>=)
This comparison operator returns True if the value on the left of the operator is equal to or greater than that on the left. A False value is returned if the value on the left is neither equal to nor greater than that on the right.
The Less than or Equal to Operator (<=)
This comparison operator returns True if the value on the left side of the operator is less than or equal to that on the right side and False if otherwise.
Comparison Operators with Floating-Point Numbers
All the comparison operators that are discussed above work in the same way with floating-point numbers. For example, the equal to (==) operator returns True if floating-point numbers are equal and False if not.
The Python comparison operators above compare the actual value that you’re seeing. However, other operators such as the “is” and “is not” identity operators compare object identities i.e if variables are pointing to the same memory address.
Using the id() function we can return the identity of all variables (objects) that we are comparing. Variables pointing to the same memory address should return the same identity while those that are not the same should return unique identities.
In the example above the identities of the variable are unique and therefore we also get False values returned. However, variables with the same value should return the same identity and a True value.
Using Python Comparison Operators with Strings
We can also use comparison operators to compare strings. For instance, the equal to (==) operator returns can be used to check if the string on the left side of the operator is equal to that on the right.
In the example above we get a False returned since the strings are not the same. However, if we change the strings to be the same we will certainly get a True value returned and both variables should have the same IDs as well.
Since the variables are the same if we use the ‘is’ identity operator we can see that we get True since the strings are the same.
However, if we modify one of the strings we can see that we get different IDs and a False value returned as well. We have modified the string stored in variable y to have an uppercase letter O.
If we convert the strings to lowercase while comparing them, we will certainly get a True boolean value returned. However, the IDs are different, this is because although we changed the strings while comparing them, the original variables remain unique and therefore their object identities are not the same.
On the other hand, if we use the identity operator ‘is’ instead of the comparison operator ‘==’ you will notice that although we have converted the strings to lowercase, the identity operator returns False. This is because the variables still point to different memory addresses.
Summary
So this is how you can use Python comparison operators in a nutshell. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
- How to use Python decorator
- How to use the Python argparse module
- How to use Python queue
- How to use Python threading
- How to concatenate strings in Python
- How to use the Python absolute value
- How to use the Python comparison operators
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
