This tutorial will teach you about Python comparison operators and how to use them to compare two values.

In programming, you’ll frequently wish to compare one value to another. You use comparison operators to accomplish this.

Python has six comparison operators:

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

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

You can compare numbers and strings using these comparison operators.

## Less than operator (<) in Python

The operator () compares two values and returns the lesser of the two. True if the leftmost value is less than the rightmost value. Otherwise, False is returned:

left_value < right_value

The following example compares two numbers using the Less Than () operator:

>>> 10 < 20 True >>> 30 < 20 False

When you apply the less than operator on numbers, it’s rather evident.

The less than operator () is used to compare two strings in the example below:

>>> 'apple' < 'orange' True >>> 'banana' < 'apple' False

Because the letter an in apple comes before the letter o in orange, the expression ‘apple’ ‘orange’ yields True.

Similarly, because the letter ‘b’ comes after the letter ‘a,’ the ‘banana’ ‘apple’ returns False.

The less than operator is demonstrated with variables in the following example:

>>> x = 10 >>> y = 20 >>> x < y True >>> y < x False

## Less than or equal to operator (<=) in Python

When two numbers are less than or equal, the less than or equal to operator returns True. It returns False otherwise:

left_value <= right_value

The following example demonstrates how to compare two numbers using the less than or equal to operator:

>>> 20 <= 20 True >>> 10 <= 20 True >>> 30 <= 30 True

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

>>> x = 10 >>> y = 20 >>> x <= y True >>> y <= x False

## Greater than operator (>) in Python

When two values are compared, the greater than operator (>) returns True if the left value is greater than the right value. Otherwise, False is returned:

left_value > right_value

The larger than operator (>) is used to compare two numbers in this example:

>>> 20 > 10 True >>> 20 > 20 False >>> 10 > 20 False

The greater than operator (>) is used to compare two strings in the following example:

>>> 'apple' > 'orange' False >>> 'orange' > 'apple' True

## Greater Than or Equal To operator (>=) in Python

The larger than or equal to (>=) operator compares two numbers and returns the greater of the two. If the left value is larger than or equal to the right value, the condition is true. Otherwise, False is returned:

left_value >= right_value

The greater than or equal to operator is used to compare two numbers in the following example:

>>> 20 >= 10 True >>> 20 >= 20 True >>> 10 >= 20 False

The following example compares two strings using the larger than or equal to operator:

>>> 'apple' >= 'apple' True >>> 'apple' >= 'orange' False >>> 'orange' >= 'apple' True

## Equal To operator (==) in Python

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

left_value = right_value

The equal to (==) operator is used to compare two numbers in the following example:

>>> 20 == 10 False >>> 20 == 20 True

The equal to operator (==) is used to compare two strings in the following example:

>>> 'apple' == 'apple' True >>> 'apple' == 'orange' False

## Not Equal To operator (!=) in Python

When two values are compared, the not equal to operator (!=) returns True if the left value is not equal to the right value. Otherwise, False is returned.

left_value != right_value

The not equal to operator is used to compare two numbers in the following example:

>>> 20 != 20 False >>> 20 != 10 True

The not equal to operator is also used to compare two strings in the following example:

>>> 'apple' != 'apple' False >>> 'apple' != 'orange' True

## Leave a Reply