by BehindJava

What are Comparison Operators in Python with Syntax and Examples

Home » python » What are Comparison Operators in Python with Syntax and Examples

In this tutorial we will be learning about Comparison Operators in Python. These operators will allow us to compare variables and output a Boolean value (True or False).

If you have any sort of background in Math, these operators should be very straight forward.

Operator Description
== If the values of two operands are equal, then the condition becomes true.
!= If values of two operands are not equal, then the condition becomes true.
> If the value of left operand is greater than the value of right operand, then the condition becomes true.
< If the value of left operand is less than the value of right operand, then the condition becomes true.
>= If the value of left operand is greater than or equal to the value of right operand, then the condition becomes true.
<= If the value of left operand is less than or equal to the value of right operand, then the condition becomes true.

Let’s now work through quick examples of each of these.

Equal

2 == 2
True
1 == 0
False
  • Note that == is a comparison operator, while = is an assignment operator.

Not Equal

2 != 1
True
2 != 2
False

Greater Than

2 > 1
True
2 >1
False

Less Than

2 < 4
True
2 < 1
False

Greater Than or Equal to

2 >= 2
True
2 >= 1
True

Less than or Equal to

2 <= 2
True
2 <= 4
True