# Python comparison operators

Guide to understand the working of comparison operators in Python

Comparison operators help us to compare variables and output a boolean value.

Below shown is a table demonstrating the different operators along with their role and example.

# Equality

`>>> 85 == 85True>>> 17 == 16False>>> 8 = 3File "<ipython-input-3-1f3f47bc060f>", line 1    8 = 3         ^SyntaxError: can't assign to literal`

Using two equal signs is necessary because if you use a single equal sign, it will give an error. This happens because a single equal sign is used to assign values to variables.

`>>> 'python' == 'python'True`

While comparing strings, capitalization also counts.

`>>> 'python' == 'Python'False`

While comparing, the data type is also taken into account.

`>>> 45 == '45'False`

In the case of comparing floating-point and integers, the data type is not taken into account.

`>>> 5 == 5.0True`

# Inequality

`>>> 67 != 67False>>> 57 != 67True`

# Greater than

`>>> 17 > 19False>>> 14 > 2True`

# Lesser than

`>>> 6 < 9True>>> 6 < 0False`

# Greater than or equal to

`>>> 8 >= 8True>>> 94 >= 65True>>> 14 >= 68False`

# Lesser than or equal to

`>>> 4 <= 4True>>> 4 <= 9True>>> 56 <= 12False`

# Chaining comparison operators

Logical operators can be used to combine comparisons. The keywords used for chaining are as follows:

1) and

2) or

3) not

# ‘and’ keyword

Suppose you want to check two conditions and make sure both are true, ‘and’ is used.

`>>> 14 < 15True>>> 15 < 16True>>> 14 < 15 < 16True>>> 14 < 15 > 16False>>> 14 < 15 and 15 < 16True>>> 14 < 15 and 15 > 16False>>>("python" == "python") and (3 == 4)False`

# ‘or’ keyword

Suppose you want to check two conditions and make sure at least one is true, ‘or’ is used.

`14 < 15 or 15 > 16True14 > 15 or 15 > 16False`

# ‘not’ keyword

It returns the opposite boolean.

`>>> 45 == 45True>>> not(45 == 45)False>>> 1 < 0False>>> not(1 < 0)True`

Refer to the notebook here.