Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Comparison Operators

python-comparison-operator

Comparison Operators in Python.

To compare two or more variables or constants that have some values, a comparison operator is used.

In six different ways, we can compare different variables or constants, compare whether two values are equal or if two values are not equal, and in several other ways.

There are total 6 comaparison operators in general:

1. Equal(==)
2. Not Equal(!=)
3. Greater than(>)
4. Less than (<)
5. Greater than equal to (>=)
6. Less than equal to(<=)

All  comparison operators in python are discussed below  in detail with their syntax and proper example:

1. Equal(==)

It compares whether two values are equal or not. If the values are found equal then it returns TRUE else it returns FALSE.

Syntax

Operand1 == Operand2

Example

>>> a = 10
>>> b = 20
>>> a == b
False
>>> 10 == 10
True
>>> a == 10
True

2. Not Equal(!=)

It compares whether two values are equal or not. If the values are found equal then it returns FALSE else it returns TRUE.

Syntax

Operand1 != Operand2

Example

>>> a = 10
>>> b = 20
>>> a != b
True
>>> 10 != 10
False
>>> a != 10
False

3. Greater than (>)

It compares whether the value at left hand side is greater than the value at right hand side.

Syntax

Operand1 > Operand2

Example

>>> a = 10
>>> b = 20
>>> a > b
False
>>> 11 > 10
True
>>> a > 15
False

4. Less than (<)

It compares whether the value at left hand side is less than the value at right hand side.

Syntax

Operand1 < Operand2

Example

>>> a = 10
>>> b = 20
>>> a < b
True
>>> 11 < 10
False
>>> a < 15
True

5. Greater than equal to(>=)

It compares whether the value at left hand side is greater than or equal to the value at right hand side.

Syntax

Operand1 >= Operand2

Example

>>> a = 10
>>> b = 20
>>> a >= b
False
>>> 11 >= 10
True
>>> a >= 15
False

6. Less than equal to(<=)

It compares whether the value at left hand side is less than or equal to the value at right hand side.

Syntax

Operand1 <= Operand2

Example

>>> a = 10
>>> b = 20
>>> a <= b
True
>>> 10 <= 10
True
>>> a <= 5
False