Comparison Operators in Python

OperatorDescription
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
==Equal to
!=Not equal to

Each element of the first object is compared with the corresponding element of the second object. The result of comparison is a Boolean value.

Let's understand these operators with the help of an example. Let's create two NumPy arrays containing returns from two stocks over the past five days.

>>> import numpy as np
>>> stock_A = np.array([10, 8, 9, 11, 12])
>>> stock_B = np.array([8, 11, 10, 10, 12])
>>> stock_A
array([10,  8,  9, 11, 12])
>>> stock_B
array([ 8, 11, 10, 10, 12])
>>>

Greater Than (>)

The greater than (>) symbol checks if each element of the first object is greater than the corresponding element of the second object. The result will be an object with logical values (TRUE or FALSE) depending on whether the condition is true or not.

In the following code, we compare if Stock A's returns are higher than Stock B's returns

>>> stock_A > stock_B
array([ True, False, False,  True, False], dtype=bool)
>>>

As you can see, Stock A's returns were higher than Stock B on day 1 (10 > 8) and day 4 (11 > 10). So, for these days, the resulting array contains TRUE, while for the rest of the days, it is FALSE. Specially observe the last day where the returns were same for both the stocks (12 and 12). Since the condition was exclusively checking for returns being greater, the condition is FALSE here.

Less Than (<)

The less than (<) symbol checks if each element of the first array is less than the corresponding element of the second array. This is just the opposite of the 'greater than' operator.

>>> stock_A < stock_B
array([False,  True,  True, False, False], dtype=bool)
>>>

Equal (==)

The equal (==) operator checks if each element of the first array is equal to the corresponding element of the second array. In our arrays, this condition meets only for the returns on the last day (12 == 12) as shown below:

>>> stock_A == stock_B
array([False, False, False, False,  True], dtype=bool)
>>>

Not Equal To (!=)

The Not Equal (!=) operator checks if each element of the first array is unequal to the corresponding element of the second array.

>>> stock_A != stock_B
array([ True,  True,  True,  True, False], dtype=bool)
>>>

Greater Than Equal To (>=)

The Greater Than Equal To (>=) operator checks if each element of the first array is greater than or equal to the corresponding element of the second array.

>>> stock_A >= stock_B
array([ True, False, False,  True,  True], dtype=bool)
>>>

Compare this with the Greater Than (>) operator. Everything else is same, just the last element now returns TRUE (because of the equal to condition).

Less Than Equal To (<=)

The Less Than Equal To (<=) operator checks if each element of the first array is less than or equal to the corresponding element of the second array.

>>> stock_A <= stock_B
array([False,  True,  True, False,  True], dtype=bool)
>>>

Related Downloads

Membership
Learn the skills required to excel in data science and data analytics covering R, Python, machine learning, and AI.
I WANT TO JOIN
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Saylient AI Logo

Take the Next Step in Your Data Career

Join our membership for lifetime unlimited access to all our data analytics and data science learning content and resources.