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.

To know more about booleans in python, read the article here.

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

Types of comparison operators (Source: Google)

Equality

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.

While comparing strings, capitalization also counts.

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

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

Inequality

Greater than

Lesser than

Greater than or equal to

Lesser than or equal to

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.

‘or’ keyword

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

‘not’ keyword

It returns the opposite boolean.

Refer to the notebook here.

Reach out to me: LinkedIn

Check out my other work: GitHub

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.