What is comparison operator in python with example?

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.

Assume variable a holds 10 and variable b holds 20, then −

OperatorDescriptionExample
== If the values of two operands are equal, then the condition becomes true. [a == b] is not true.
!= If values of two operands are not equal, then condition becomes true. [a != b] is true.
If values of two operands are not equal, then condition becomes true. [a b] is true. This is similar to != operator.
> If the value of left operand is greater than the value of right operand, then condition becomes true. [a > b] is not true.
< If the value of left operand is less than the value of right operand, then condition becomes true. [a < b] is true.
>= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. [a >= b] is not 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 is comparison operator with example?

If either of the operands is a string value, then a string comparison is used. Enclose strings in quotes ["string"]. Caution! The == operator is a comparison operator. ... .

What is Python comparison operation?

Comparison operators can compare numbers or strings and perform evaluations. Expressions that use comparison operators do not return a number value as do arithmetic expressions. Comparison expressions return either 1 , which represents true, or 0 , which represents false.

What are all the comparison operators in 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 [ != ].

Which operator is known as comparison operator in Python?

Relational Operators in Python.

Chủ Đề