Python Comparison Operators

Summary: in this tutorial, you’ll learn about Python comparison operators and how to use them to compare two values.

Introduction to Python comparison operators #

In programming, you often want to compare a value with another value. To do that, you use comparison operators.

Python has six comparison operators, which are as follows:

  • Less than ( < )
  • Less than or equal to (<=)
  • Greater than (>)
  • Greater than or equal to (>=)
  • Equal to ( == )
  • Not equal to ( != )

These comparison operators compare two values and return a boolean value, either True or False.

You can use these comparison operators to compare both numbers and strings.

Less than operator (<) #

The Less Than operator (<) compares two values and returns True if the value on the left is less than the value on the right. Otherwise, it returns False:

left_value  < right_valueCode language: Python (python)

The following example uses the Less Than (<) operator to compare two numbers:

x = 10
y = 20

result = x < y
print(result)

result = x > y
print(result)Code language: Python (python)

Try it

Output:

True
FalseCode language: PHP (php)

It’s quite obvious when you use the less-than operator with the numbers.

The following example uses the less than operator (<) to compare two strings:

result = 'apple' < 'orange'
print(result)

result = 'banana' < 'apple'
print(result)Code language: Python (python)

Try it

Output:

True
FalseCode language: PHP (php)

The expression 'apple' < 'orange' returns True because the letter a in apple is before the letter o in orange.

Similarly, the 'banana' < 'apple' returns False because the letter 'b' is after the letter 'a'.

The following example shows how to use the less-than operator with variables:

x = 10
y = 20

result = x < y
print(result)

result = y < x
print(result)Code language: Python (python)

Try it

Output:

True
FalseCode language: PHP (php)

Less than or equal to operator (<=) #

The less than or equal to operator compares two values and returns True if the left value is less than or equal to the right value. Otherwise, it returns False:

left_value <= right_valueCode language: Python (python)

The following example shows how to use the less than or equal to operator to compare two numbers:

result = 20 <= 20
print(result)

result = 10 <= 20
print(result)

result = 30 <= 30
print(result)Code language: Python (python)

Try it

Output:

True
True
TrueCode language: PHP (php)

This example shows how to use the less than or equal to operator to compare the values of two variables:

x = 10
y = 20

result = x <= y
print(result)

result = y <= x
print(result)Code language: Python (python)

Try it

Output:

True
FalseCode language: PHP (php)

Greater than operator (>) #

The greater than the operator (>) compares two values and returns True if the left value is greater than the right value. Otherwise, it returns False:

left_value > right_valueCode language: Python (python)

This example uses the greater than operator (>) to compare two numbers:

result = 20 > 10
print(result)

result = 20 > 20
print(result)

result = 10 > 20
print(result)Code language: Python (python)

Try it

Output:

True
False
FalseCode language: PHP (php)

The following example uses the greater than operator (>) to compare two strings:

result = 'apple' > 'orange'
print(result)

result = 'orange' > 'apple'
print(result)Code language: Python (python)

Try it

Output:

False
TrueCode language: PHP (php)

Greater Than or Equal To operator (>=) #

The greater than or equal to operator (>=) compares two values and returns True if the left value is greater than or equal to the right value. Otherwise, it returns False:

left_value >= right_valueCode language: Python (python)

The following example uses the greater than or equal to an operator to compare two numbers:

result = 20 >= 10
print(result)

result = 20 >= 20
print(result)

result = 10 >= 20
print(result)Code language: Python (python)

Try it

Output:

True
True
FalseCode language: PHP (php)

The following example uses the greater than or equal to operator to compare two strings:

result = 'apple' >= 'apple'
print(result)

result = 'apple' >= 'orange'
print(result)

result = 'orange' >= 'apple'
print(result)Code language: Python (python)

Try it

Output:

True
False
TrueCode language: PHP (php)

Equal To operator (==) #

The equal to operator (==) compares two values and returns True if the left value is equal to the right value. Otherwise, it returns False :

left_value == right_valueCode language: Python (python)

The following example uses the equal to operator (==) to compare two numbers:

result = 20 == 10
print(result)

result = 20 == 20
print(result)Code language: Python (python)

Try it

Output:

False
TrueCode language: PHP (php)

And the following example uses the equal to operator (==) to compare two strings:

result = 'apple' == 'apple'
print(result)

result = 'apple' == 'orange'
print(result)
Code language: Python (python)

Try it

Output:

True
FalseCode language: PHP (php)

Not Equal To operator (!=) #

The not equal to operator (!=) compares two values and returns True if the left value isn’t equal to the right value. Otherwise, it returns False.

left_value != right_valueCode language: Python (python)

For example, the following uses the not equal to operator to compare two numbers:

result = 20 != 20
print(result)

result = 20 != 10
print(result)Code language: Python (python)

Try it

Output:

False
TrueCode language: PHP (php)

The following example uses the not equal to operator to compare two strings:

result = 'apple' != 'apple'
print(result)

result = 'apple' != 'orange'
print(result)Code language: Python (python)

Try it

Output:

False
TrueCode language: PHP (php)

Summary #

  • A comparison operator compares two values and returns a boolean value, either True or False.
  • Python has six comparison operators: less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), and not equal to (!=).

Quiz #

Was this tutorial helpful ?