[Python tutorial] Chapter 13 comparison operators

Posted by kingleo on Wed, 05 Jan 2022 18:37:20 +0100

In this article, we will introduce the Python comparison operator and how to use the comparison operator to compare the size of two data.

Comparison operator

In the process of programming, we often need to compare the size of two values. For this purpose, we can use the comparison operator.

Python provides the following six comparison operators:

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

The above comparison operators can be used to compare the size of two values and return a Boolean value of True or False.

Comparison operators can be used to compare two numbers or two strings.

Less than (<) operator

The less than (<) operator can compare the size of two values. When the data on the left is less than the data on the right, it returns True, otherwise it returns False:

left_value  < right_value

The following example uses the less than operator to compare the size of two numbers:

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

The comparison of numbers is easy to understand. The following example compares two strings using the less than operator:

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

The expression 'apple' < 'orange' returns True because the first letter a of the string 'apple' is in front of the first letter o of the string 'orange'. Similarly, the expression 'banana' < 'apple' returns False because the letter b is in later order than the letter A.

The order of upper case letters is higher than that of lower case letters, so 'Banana' < 'apple' returns True.

The following example compares the values of two variables using the less than operator:

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

Less than or equal to (< =) operator

The less than or equal (< =) operator can compare the size of two values. When the data on the left is less than or equal to the data on the right, it returns True, otherwise it returns False:

left_value <= right_value

The following example uses the less than or equal operator to compare the size of two numbers:

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

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

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

Greater than (>) operator

The greater than (>) operator can compare the size of two values. When the data on the left is greater than the data on the right, it returns True, otherwise it returns False:

left_value > right_value

The following example compares the size of two numbers using the greater than operator:

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

The following example compares two strings using the greater than operator:

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

Greater than or equal to (> =) operator

The greater than or equal (> =) operator can compare the size of two values. When the data on the left is greater than or equal to the data on the right, it returns True, otherwise it returns False:

left_value >= right_value

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

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

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

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

Equal to (= =) operator

The equal (= =) operator can compare the size of two values. When the data on the left is equal to the data on the right, it returns True, otherwise it returns False:

left_value = right_value

The following example compares the size of two numbers using the equals operator:

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

The following example compares two strings using the equals operator:

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

Not equal to (! =) operator

The not equal (! =) operator can compare the size of two values. When the data on the left is not equal to the data on the right, it returns True, otherwise it returns False:

left_value != right_value

The following example compares the size of two numbers using the not equal operator:

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

The following example compares two strings using the not equal operator:

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

summary

  • The comparison operator can compare the size of two data and return Boolean values True or False.
  • Python provides six comparison operators: less than (<), less than or equal to (< =), greater than (>), greater than or equal to (> =), equal to (= =), and not equal to (! =).

Topics: Python