Comparison Operators in Python Language

Comparison Operators

In programming comparison operators are used to compare two or more values. These are comparison operators used in Python.

  1. Equals To – “==”

To check whether one value is equal to other value.  The answer is returned in Boolean format.

Example:

>>> name = “Mohit”  # Assignment

>>> name == “Mohit”  # Comparison

True  # Output

>>> x = 5  # Assigment

>>> 5 == x  # Comparsion – LHS is equal to RHS here

True

 

*Use of If Condition

Syntax of if

if(condition):

            statement

Example

>>> name = “Ramesh”

>>> if(name == “Ramesh”):

#indentation block

print(“Welcome Ramesh Babu”)

Welcome Ramesh Babu  #output

  1. Greater than: “ > “

>>> x = 67

>>> if(x > 65):

            print(x, ” is greater than 65″)

67 is greater than 65

Example with With if and else :

x = 68

if(x > 65):

            print(x , ” is g t 65 “)

else:

            print(x , ” is ngt 65 “)

# 68 is g t 65

  1. Greater than or equal to

Example :

>>> t = 678   # assignment

>>> t >= 789 # comparison

False              # output

>>> t >= 678

True

>>> t >= 677

True

  1. Less Than

>>> cb = 56

>>> cb < 45

False

  1. Less than or equal to

>>> cb = 56

>>> cb <=  56

True

>>> cb <=  78

True

 

Leave a Comment