Quick Tip Using Pythons Comparison Operators

This Quick Tip Using Python’s Comparison Operators Tutorial assists python learners to learn about the comparison operators called boolean operators. In python or any other programming language, the Boolean data type can be one of two values, either True or False.

Majorly, we utilize Booleans in various programming languages to create some comparisons and to control the flow of the program. In this Python Comparison (Relational) Operator Tutorial, we are going to discuss the basics where you’ll understand how Booleans work exactly with examples.

The tutorial of python’s comparison operators consists of the following modules: 

Comparison Operators | Understanding Boolean Logic in Python

Exactly similar to any other OOP language, Python utilizes the comparison operators to compare values. These are usually termed Boolean operators because the word Boolean indicates that the outcome of using the comparison operator is a Boolean value: true or false. The following information is a list of the Boolean values that can be used in Python when evaluating expressions, writing functions, and comparing values. Take a look at the below table:

Operator What it means
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

1. Python Less Than (<) Operator

< this one indicates that you’re comparing two values to see if the first value is less than the second, like this:

7 < 10

So here, your answer would also be your Boolean value: true.

2. Python Greater Than (>) Operator

this operator indicates that you’re comparing two values to see if the first one is greater than the second. So in the same example as above with the greater than sign replacing the lefthand side, you’d get a Boolean result of false, because 7 is not greater than 10.

3. Python Less Than or Equal To (<=) Operator

<= this one indicates that you’re comparing two values to see if the first is less than or equal to the second. So if you have the following expression:

10 <= 10

your answer would be the Boolean value true because 10 is equal to 10 (it’s not less than ten, but we’re checking for less than OR equal to)

4. Python Greater Than or Equal To (>=) Operator

>= you can probably guess what this operator is, but just in case you can’t, it’s used to indicate that you’re comparing two values to see if the second is greater than or equal to the first. So if you switched the signs in the example above from less than or equal to to greater than or equal to, you’d still get the same true value because 10 is still equal to 10.

>> from math import pi
>>> 3.14>=pi

Output

False

5. Python Equal To (==) Operator

== indicates that you’re trying to discern if two values are equal to each other — be sure to use two equals signs, not one!

 {1,3,2}=={1,2,3}

Result:

True

6. Python Not Equal Operator (!=) Operator

!= indicates that you’re trying to discern if two values are not equal to each other. So if you had an expression like:

7 != 10

your answer would be the Boolean value: true.

Also Read: 

Python Comparison Operators Example

Comparison operators are typically used in Boolean contexts like conditional and loop statements to direct program flow.

#!/usr/bin/python

a = 21
b = 10
c = 0

if ( a == b ):
   print "Line 1 - a is equal to b"
else:
   print "Line 1 - a is not equal to b"

if ( a != b ):
   print "Line 2 - a is not equal to b"
else:
   print "Line 2 - a is equal to b"

if ( a <> b ):
   print "Line 3 - a is not equal to b"
else:
   print "Line 3 - a is equal to b"

if ( a < b ):
   print "Line 4 - a is less than b" 
else:
   print "Line 4 - a is not less than b"

if ( a > b ):
   print "Line 5 - a is greater than b"
else:
   print "Line 5 - a is not greater than b"

a = 5;
b = 20;
if ( a <= b ):
   print "Line 6 - a is either less than or equal to  b"
else:
   print "Line 6 - a is neither less than nor equal to  b"

if ( b >= a ):
   print "Line 7 - b is either greater than  or equal to b"
else:
   print "Line 7 - b is neither greater than  nor equal to b"

Output: 

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b

Equality Comparison on Floating-Point Values

Remembering the previous discussion of floating-point numbers that the element stored internally for a float object can’t be precisely compared. For that reason, it is poor practice to compare floating-point values for accurate equality. Take a look at this example:

>>> x = 1.1 + 2.2
>>> x == 3.3
False

The internal illustrations of the addition operands are not precisely equal to 1.1 and 2.2, so you cannot rely on x to compare exactly to 3.3.

The approved way to resolve whether two floating-point values are “equal” is to compute whether they are close to one another, given some tolerance. Consider this example:

>>> tolerance = 0.00001
>>> x = 1.1 + 2.2
>>> abs(x - 3.3) < tolerance
True

Absolute value is returned by the abs(). If the absolute value of the difference between the two numbers is less than the specified tolerance, they are close enough to one another to be considered equal.

Python Chained Comparison | Chaining in Comparison Operators

  1. Comparisons return boolean values: True or False.
  2. Comparisons can be chained arbitrarily. For example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
  3. Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c … y opN z is equivalent to a op1 b and b op2 c and … y opN z, except that each expression is evaluated at most once.
  4. Also, a op1 b op2 c doesn’t imply any kind of comparison between a and c, so a < b > c is perfectly legal.

Python Program to illustrate Chaining Comparison Operators in Python

x = 5
print(1 < x < 10)
print(10 < x < 20 )
print(x < 10 < x*10 < 100)
print(10 > x <= 9)
print(5 == x > 4)

Output: 

True
False
True
True
True

Python Interview Questions on Comparison (Relational) Operators

A few interview questions on the Python comparison operators concept are listed below for beginners and freshers who are looking for jobs in the Python programming field:

  1. How many comparison operators are there in Python?
  2. What are relational operators in Python?
  3. What are the different types of operators in Python?
  4. How do you use greater than in Python?
  5. How does the operator work in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *