Python Comparison Operators – with example

  • Reading time:19 mins read

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.

 

python comparison operator

 

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.

 

Python comparison operator

 

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

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.