Python Comparison Operators

Python Comparison Operators

Python Comparison Operators

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

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 PythonExample
Equal==x == y
Not equal!=x != y
Greater than>x > y
Less than<x < y
Greater than or equal to>=x >= y
Less than or equal to<=x <= y

Python 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

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.

Python 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

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.

Python 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

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.

Python 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

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.

Python 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

Output

True
True
False

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

Python 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

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.