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.

You may find these interesting

Operational Risk Data
For any bank, the measurement and management of operational risk is of prime importance. One of the...
Open Market Operations
The central bank of a country employs open market operations to implement the monetary policy. In th...
Types of Operational Risk
The financial institutions encounter a variety of operational risks on a daily basis. It’s important...

Related Downloads

Finance Train Premium
Accelerate your finance career with cutting-edge data skills.
Join Finance Train Premium for unlimited access to a growing library of ebooks, projects and code examples covering financial modeling, data analysis, data science, machine learning, algorithmic trading strategies, and more applied to real-world finance scenarios.
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

Accelerate your finance career with cutting-edge data skills.

Join Finance Train Premium for unlimited access to a growing library of ebooks, projects and code examples covering financial modeling, data analysis, data science, machine learning, algorithmic trading strategies, and more applied to real-world finance scenarios.