- 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.
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
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $39 (Regular $57)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.