Python Comparison Operators

By | July 18, 2019

Python Comparison Operators compare two values and gives a single Boolean value as a result. For example, suppose we wish to check whether two variables a and b are equal. We will use the equal operator represented by ‘==’.

a=10
b=20
a==b     will gives False since 10 is not equal to 20
Similarly, if a=10 and b=10, then a==b will return True.
How to use Comparison operators in Python

Here is a list of python comparison operators with their symbols:

> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal 
!= not equal 

How to use Comparison Operators in Python: Examples

Write a Python if statement to check whether a given variable is equal to zero or not

n=0
if n==0:
   print('Number is Zero')
else:
   print('Not Zero');
The output will be:
Number is Zero
Since if-else statement will evaluate the given condition n==0
and will get a result True. 
Therefore, it executes if-block and prints 'Number is Zero' 
on the screen.

Python Script To Check Which of the Two Numbers is greater than the other:

 n1=100
 n2=200
 if n1>n2:
     print('First number is greater than second')
 elif n2>n1:
     print('Second number is greater than first')
 else:
     print('Both numbers are equal')

The output will be:
 Second number is greater than first 

Loading

Leave a Reply

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