Python Comparison Operators

Python Comparison Operators

Python Comparison Operators compare given two operand values and return a boolean value based on the comparison made.

Python Comparison Operators

There are six comparison operators in Python. They are

  • Equal
  • Not equal
  • Greater than
  • Less than
  • Greater than or equal to
  • Less than or equal to

Following table demonstrates the symbols used for these comparison operators, and a simple example to demonstrate the usage.

Comparison OpertorOperator symbol in PythonExampleDescription
Equal==x == yReturns True if both x and y are equal in value.
Not equal!=x != yReturns True if x and y are not equal in value.
Greater than>x > yReturns True if x is greater than y in value.
Less than<x < yReturns True if x is less than y in value.
Greater than or equal to>=x >= yReturns True if x is greater than or equal to y in value.
Less than or equal to<=x <= yReturns True if x is less than or equal to y in value.

1. Equal Operator

Python Equal Operator compares if the two operands are equal or not. If the operands are equal, the operator returns true, else, it returns false.

Following is a simple Python program, where we compare two numbers, if they are equal.

Python Program

x = 12
y = 12
result = x == y
print(result)

x = 8
y = 7
result = x == y
print(result)
Run Code Copy

Output

True
False

In the first case, x==y returns True, since x and y hold same integer value.

In the second case, x==y returns False, since x and y hold different integer values.

More about equal operator in Python Equal Operator.

2. Not Equal Operator

Python Not Equal Operator compares if the two operands are not equal. If the operands are not equal, the operator returns true, else, it returns false.

Following is a simple Python program, where we compare two numbers, if they are not equal.

Python Program

x = 8
y = 7
result = x != y
print(result)

x = 12
y = 12
result = x != y
print(result)
Run Code Copy

Output

True
False

In the first case, x!=y returns True, since x and y hold different integer values.

In the second case, x!=y returns False, since x and y hold same integer value.

More about equal operator in Python Not Equal Operator.

3. Greater than Operator

Python Greater than Operator compares if the left operand is greater than the right side operand. If the left operand is greater than right operand, the operator returns True, else, it returns False.

Following is a simple Python program, where we compare two numbers, if operand x is greater than operand y.

Python Program

x = 8
y = 7
result = x > y
print(result) #True

x = 5
y = 12
result = x > y
print(result) #False
Run Code Copy

Output

True
False

In the first case, x>y returns True, since x holds 8 which is greater than y which holds 7.

In the second case, x>y returns False, since x holds 5 which is not greater than y which holds 12.

More about equal operator in Python Greater than Operator.

4. Less than Operator

Python Less than Operator compares if the left side operand is less than the right side operand. If the left operand is less than right operand, the operator returns True, else, it returns False.

Following is a simple Python program, where we compare two numbers, if operand x is less than operand y.

Python Program

x = 3
y = 7
result = x < y
print(result) #True

x = 75
y = 12
result = x < y
print(result) #False
Run Code Copy

Output

True
False

In the first case, x<y returns True, since 3 is less than 7.

In the second case, x>y returns False, since 75 is not less than 12.

More about equal operator in Python Less than Operator.

5. Greater than or Equal to Operator

Python Greater than or Equal to Operator compares if the operand left to it is greater than or equal to the right side operand.

Following is a simple Python program, to demonstrate greater than or equal to operator.

Python Program

x = 13
y = 7
result = x >= y
print(result) #True

x = 12
y = 12
result = x >= y
print(result) #True

x = 12
y = 75
result = x >= y
print(result) #False
Run Code Copy

Output

True
True
False

More about equal operator in Python Greater than or Equal to Operator.

6. Less than or Equal to Operator

Python Less than or Equal to Operator compares if the operand left to it is less than or equal to the right side operand or not. The result would be True or False respectively.

Following is a simple Python program, to demonstrate less than or equal to operator.

Python Program

x = 13
y = 72
result = x <= y
print(result) #True

x = 12
y = 12
result = x <= y
print(result) #True

x = 12
y = 5
result = x <= y
print(result) #False
Run Code Copy

Output

True
True
False

More about equal operator in Python Less than or Equal to Operator.

Summary

In this tutorial of Python Examples, we learned about Comparison Operators in Python, and a detailed understanding on each of the operators with examples programs.

Related Tutorials

Code copied to clipboard successfully 👍