Skip to main content

Python Relational Operators

Operators are used to do action or process on variables and operands. The operator usually represents  as symbol for doing an action. Python has different type of operators like Arithmetic, Assignment, Relational / Comparison , Logical, Bitwise, Identity and Membership operators. Every operators are using  to do different kind of operations.

In this tutorial we will learn about  Python Relational Operators.

Python Relational Operators

Python Relational operators are used to compare the values between two operand. it returns either true or false.

 
Operator Description Syntax Example
== If both operands are equal, It returns true otherwise false var1 == var2 a == b
!= If both operands are not equal, It returns true otherwise false var1 != var2 a != b
> If first operand greater than second operand, It returns true otherwise false var1 > var2 a > b
< If first operand less than second operand,  It returns true otherwise false var1 < var2 a < b
>= If first operand greater than and equal to second operand, It returns true otherwise false var1 >= var2 a >= b
<= If first operand less than and equal second operand, It returns true otherwise false var1 <= var2 a <= b

Code Example:-

>>> var1 = 2
>>> var2 = 2
>>> print('Equal To => ' + str(var1) + ' == ' +  str(var2) + ' = ' + str(var1 == var2))
Equal To => 2 == 2 = True
>>> var1 = 2
>>> var2 = 3
>>> print('Not Equal To => ' + str(var1) + ' != ' + str(var2) + ' = ' + str(var1 != var2))
Not Equal To => 2 != 3 = True
>>> var1 = 6
>>> var2 = 5
>>> print('Greater Than => ' + str(var1) + ' > ' + str(var2) + ' = ' + str(var1>var2))
Greater Than => 6 > 5 = True
>>> var1 = 6
>>> var2 = 5
>>> print('Greater Than or Equal To => ' + str(var1) + ' >= ' + str(var2) + ' = ' + str(var1>=var2))
Greater Than or Equal To => 6 >= 5 = True
>>> var1 = 3
>>> var2 = 5
>>> print('Less Than => ' + str(var1) + ' < ' + str(var2) + ' = ' + str(var1<var2))
Less Than => 3 < 5 = True
>>> var1 = 3
>>> var2 = 5
>>> print('Less Than or Equal To => ' + str(var1) + ' <= ' + str(var2) + ' = ' + str(var1<=var2))
Less Than or Equal To => 3 <= 5 = True
>>>