- 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
Logical Operators in Python
In programming, you will often come across situations where you will need to evaluate multiple conditions. For example, you may want to perform an action when the price of a stock is above 10 but below 12. Logical operators come into play in such situations. These logical operators allow a program to make a decision based on multiple conditions.
There are three logical operators: and, or and not.
and (&)
This is called the logical 'and' operator. The logical 'and' operator (&) returns the boolean value true if both operands are true and returns false otherwise. The following examples illustrate this:
#Assign values to Stock A and Stock B
stock_A = 10
stock_B = 12
#Test if Stock A is equal to 10 and Stock B is equal to 12.
(stock_A==10) & (stock_B==12)
This will return True if both the conditions evaluate to True. The result will be True as shown below:
> (stock_A==10) & (stock_B==12)
[1] True
However, how will this work if we apply this to Numpy arrays.
# The following are two arrays containing returns from two stocks over the past five days.
stock_A = np.array([10, 8, 9, 11, 12])
stock_B = np.array([8,11,10,10,12])
Let's say we want to check if Stock A exceeds 10 (Inclusive) and Stock B is below 10 (Inclusive).
The conditions are satisfied at element level. For the first element, both conditions are true (Stock A: 10>=10 and Stock B: 8 <=10). Since we are using and operator, both conditions should be True, which it is in our case, so, for the first element, the result will be True. For the 2nd element both conditions are False. Stock A value is 8 which is not greater than or equal to 10. Similarly for Stock B actual value is 11, which is not less than or equal to 10 as required by our condition. Since both conditions are False, the result will be False. The only time the result will be True will be when both the conditions are True.
For Numpy arrays, we use the function logical_and()
for the logical 'and' operator as shown below:
The results are shown below:
In [1]: np.logical_and(stock_A >=10, stock_B <=10)
Out [1]: array([ True, False, False, True, False], dtype=bool)
or (|)
This is called the logical 'or' operator. The logical 'or' operator (|) returns the boolean value true if either operand is true and returns false otherwise.
> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
>
> #Test if Stock A is equal to 10 or Stock B is equal to 12.
> (stock_A==10) | (stock_B==15)
[1] True
For applying the 'or' operator to Numpy arrays, we use the np.logical_or()
function.
> # The following are two arrays containing returns from two stocks over the past five days.
>
> stock_A = np.array([10, 8, 9, 11, 12])
> stock_B = np.array([8,11,10,10,12])
>
> # Stock A exceeds 10 (Inclusive) OR Stock B is below 10 (Inclusive)
>
> np.logical_or(stock_A >=10,stock_B <=10)
array([ True, False, True, True, True], dtype=bool)
>
not
This is called logical 'not' operator. It takes each element of the object and gives the opposite logical value.
> #Assign values to Stock A and Stock B
> stock_A = 10
> stock_B = 12
>
> #Examples of logical not operator
> not ((stock_A==10) | (stock_B==15))
[1] False
> not True
[1] False
> not 5>10
[1] True
To apply the not operator to Numpy arrays, we use the np.logical_not
function.
Now that we know about relational and logical operators, we can move to the next topic - Conditional Statements.
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 $29 (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.