Skip to content

Python Comparison Operators

python comparison operators

Python comparison operators compares any two variables or values. Comparison operators always returns a boolean value . All comparison operators and their application are shown below in the form of a table taking the value of x and y as 9 and 2

Operator(name)ExampleDescriptionResult
==(Equal to)x == yReturns True if both operands are equal else FalseFalse
!=(Not equal to)x != yReturns True if both operands are not equal else FalseTrue
>(Greater than)x > yReturns True if left operand is greater than right operand else FalseTrue
>=(Greater than or equal to)x >= yReturns True if left operand is greater than or equal to right operand else FalseTrue
<(Less than)x < yReturns True if left operand is less than right operand else FalseFalse
<=(Less than or equal to)x <= yReturns True if left operand is less than or equal to right operand else FalseFalse

Now we will see about all the comparison operators in details with code –

Python Equal to operator

Python Equal to(“==”) operator returns True if both the operands are equal else returns False .

Example 1:

x=9
y=2
print("x==y =",x==y)
x==y = False

In the above code value of x and y are 9 and 2.As the value of x and y are different , x==y returned False .

Python Not equal to operator

Python Equal to(“!=”) operator returns True if both the operands are different else returns False .

Example 1:

print("x!=y =",x!=y)
x!=y = True

In the above code as the value of x(9) and y(2)  are different , x!=y returned True .

Python Greater than operator

Python Greater than(“>”) operator returns True if left operand is greater than right operand else returns False .

Example 1:

print("x>y =",x>y)
x>y = True

In the above code as the value of x(9) is greater than the value of y(2) , x>y returned True .

Python Greater than or equal to operator

Python Greater than or equal to(“>=”) operator returns True if left operand is greater than right operand or left operand is equal to right operand else returns False .

Example 1:

print("x>=y =",x>=y)
x>=y = True

In the above code as the value of x(9) is greater than the value of y(2) , x>=y returned True

Python Less than operator

Python Less than(“<”) operator returns True if left operand is less than the right operand else returns False .

Example 1:

print("x<y =",x<y)
x<y = False

In the above code as the value of x(9) is not less than the value of y(2) , x>=y returned False .

Python Less than or equal to operator

Python Less than or equal to(“<=”) operator returns True if left operand is less than the right operand or left operand is equal to the right operand else returns False .

Example 1:

print("x<=y =",x<=y)
x<=y = False

In the above code as the value of x(9) is neither less than the value of y(2) nor equal to the value of y(2) , x<=y returned False .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply