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.

Comparison Operators in Python

There are total 6 Comparison 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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription