Python Comparison Operators

Comparison is a fundamental part of our lives to make decisions. For e.g, if you get 2 apples at 5 or you can get 3 apples at 5.25!

What would you buy? 3 apples, right?

We make decisions the same way, right? This is just an example. We make hundreds of decisions by comparing multiple things. Our brain is subconsciously doing the calculation with > (greater than) or < (less than) or many more. Comparison operators are used to comparing values.

Similarly, if we want computers to make choices according to some conditions, these comparison operators are essential to telling computers how to make these decisions. When we compare something in our brain, the result can be Yes/No, True/False Pass/Fail and Go/Stop. Computers, after comparing can only result in either True or False binary operators.

So for a self-driving car, there must be an engineer sitting at a desk and telling the car’s operating system-

if signal == red then "Stop!"

See how we teach computers to make decisions; without comparison operators, it is impossible!

It returns either Binary operators either True or False according to the condition. If we put the above car example in the same context; if the signal is red, i.e signal == red; then it returns True and the car stops.

Comparison operators are very easy to understand. It just needs a quick read and you are good because we all have used this at times in our lives if not in programming.

Let’s understand the types of comparison operators with some easy examples.

Equal to operator (==)

If the values of two operands are equal, then the condition becomes true.
(a == b) is not true.

remember single equal to (=) is the assignment operator. a = b would mean we are assigning value b to variable a.

Let’s check how it works!

a, b = 10, 20

a == b

"""
output:

False

"""

 

10 == 10.0

"""
output:

True

"""

 

Not Equal to operator (!=)

This is the opposite of Equal to operator. It checks whether the left operand is not equal to the right operand.

Let’s try the same previous examples with !=

a, b = 10, 20

a != b

"""

output:

True

"""

 

In the above example, a is not equal to b so a not equal to b stands true. 🙂

10 != 10.0

"""
output:

False

"""

 

Important note: (<>)

In some programming languages (a <> b) is also considered as ‘a’ not equals to ‘b’ which is the same as (a != b). **While this was also valid in Python 2 but in Python 3, it is not. 

Make sure to always use the!= operator in Python for ‘not equals comparisons. It has to be clear because it works in SQL and some other programming languages but not in Python. 

 

Greater than operator (>)

Greater than operator check if the value of the left operand is greater than the right one.

for e.g

if ind(score)> SL (score) ; india wins!

Let’s try the same previous examples with greater than operator!

a = 10
b = 20

a > b

"""
output:

False

"""

 

b > a

"""
output:

True

"""

 

Less than operator (<)

Less than operator is opposite to what greater than is. It checks if the value of the left operand is less than the right one.

Let’s try the same previous examples with less than operator!

a = 10
b = 20

a < b

"""
output:

True

"""

 

b < a

"""
output:

False

"""

 

Greater than or Equal to Operator (>=)

As we understand from the name itself, it checks if the left operand is either greater than or equal to the right operand. if any of the conditions is true, it will return True

Let’s try one example!

a = 10
b = 20
c = 5

a >= c

"""
output:

True

"""

 

b >= c

"""
output:

True

"""

 

Less than or Equal to Operator (<=)

It checks if the left operand is either less than or equal to the right operand. if any of the conditions is true, it will return True

Let’s try one example!

a = 10
b = 20

a <= b

"""
output:

True

"""

 

Try to understand the following examples for a better grip over comparison operators!

x = 10
y = 12

print(f"x  = {x} and y = {y}")
# Output: x > y is False
print('x > y is',x>y)

# Output: x < y is True
print('x < y is',x<y)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is False
print('x >= y is',x>=y)

# Output: x <= y is True
print('x <= y is',x<=y)

"""
output:

x  = 10 and y = 12
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True

"""

 

Float comparison

Equality Comparison on Floating-Point Values Recalls from the earlier discussion of floating-point numbers, that the value stored internally for a floating object may not be precisely what you’d think it would be. For that reason, it is poor practice to compare floating-point values for exact equality.

Consider this example:

x = 1.1 + 2.2

"""
output:

x
3.3000000000000003

"""

 

x == 3.3

"""
output:

False

"""

 

In the blog, we have understood how comparison operators are used with some examples.

These operators will be fundamental when we write programs to instruct the computer to do something based on a set of conditions. We will use and practice these in the next blogs.