Geek University

Comparison operators

As their name suggests, the comparison operators in Python are used to compare one value to another. The result of a comparison is a Boolean value, which can be either True or False. The following comparison operators exist in Python:

  • == – determines whether two values are equal. Examples: 1 == 1 is True, 1 == 2 is False.
  • != – determines whether two values are not equal. Examples: 1 != 2 returns True, 1 != 1 returns False.
  • > – determines if the value of the left operand is greater than the value of the right operand. Examples: 3 > 1 is True, 1 > 2 is False.
  • < – determines if the value of the left operand is less than the value of the right operand. Examples: 1 < 2 is True, 3 < 2 is False.
  • >= – determines if the value of the left operand is greater than or equal to the value of the right operand. Examples: 3 >= 1 is True, 1 >= 2 is False.
  • <= – determines if the value of the left operand is less than or equal to the value of the right operand. Examples: 1 <= 3 is True, 3 <= 2 is False.

 

Here are a couple of examples:

>>> x = 5
>>> y = 7
>>> x == y
False
>>> x != y
True
>>> x > y
False
>>> x < y
True
>>> x >= y
False
>>> x <= y
True
>>> x + y == 12
True
>>> x + y < 15
True
>>> x + y > 20
False
>>> x + y <= 12
True

Subscribe to our Newsletter

Like us on Facebook

Share on Twitter

top
Developed by Signum Soft