- Python Dictionaries
- Comparison Operators in Python
- Logical Operators in Python
- Conditional Statements in Python
- For Loop in Python
- While Loop in Python
- How to loop over python dictionaries and Numpy arrays
- What is NumPy in Python
- ndarray - Methods and Data Type
- NumPy - Methods to Create Arrays
- Python NumPy - Numerical Operations on Arrays
- Python NumPy - Indexing and Slicing Arrays

# Comparison Operators in Python

Operator | Description | |
---|---|---|

< | 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.

# This content is for paid members only.

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