What are the six comparison operators in python

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 [ =]
  • Equal to [ == ]
  • Not equal to [ != ]

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

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

Less than operator [ 10 < 20 True >>> 30 < 20 False

Code language: Python [python]

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

The following example uses the less than operator [>> 'apple' < 'orange' True >>> 'banana' < 'apple' False

Code language: Python [python]

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 >>> x < y True >>> y < x False

Code language: Python [python]

Less than or equal to operator [> 20 >> 10 >> 30 >> x = 10 >>> y = 20 >>> x >> y ]

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

left_value > right_value

Code language: Python [python]

This example uses the greater than operator [>] to compare two numbers:

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

Code language: Python [python]

And the following example uses the greater than operator [>] to compare two strings:

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

Code language: Python [python]

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_value

Code language: Python [python]

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

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

Code language: Python [python]

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

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

Code language: Python [python]

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_value

Code language: Python [python]

The following example uses the equal to operator [==] to compare two numbers:

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

Code language: Python [python]

And the following example uses the equal to operator [==] to compare two strings:

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

Code language: Python [python]

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_value

Code language: Python [python]

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

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

Code language: Python [python]

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

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

Code language: Python [python]

Summary

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

Did you find this tutorial helpful ?

What are the 6 comparison operators of Python?

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 or equal to [ >= ], equal to [ == ], and not equal to [ != ].

What is the comparison operators in Python?

Python Comparison Operators.

How many comparison operators are available in Python?

Types of Comparison Operators in Python. There are 6 types of comparison operators : Less Than [ < ] Greater Than [ > ]

What are the 7 operators in Python?

Python Operators.
Arithmetic operators..
Assignment operators..
Comparison operators..
Logical operators..
Identity operators..
Membership operators..
Bitwise operators..

Chủ Đề