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:
1#Assign values to Stock A and Stock B
2stock_A = 10
3stock_B = 12
4#Test if Stock A is equal to 10 and Stock B is equal to 12.
5(stock_A==10) & (stock_B==12)
6
This will return True if both the conditions evaluate to True. The result will be True as shown below:
1> (stock_A==10) & (stock_B==12)
2[1] True
3
However, how will this work if we apply this to Numpy arrays.
1# The following are two arrays containing returns from two stocks over the past five days.
2stock_A = np.array([10, 8, 9, 11, 12])
3stock_B = np.array([8,11,10,10,12])
4
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:
1In [1]: np.logical_and(stock_A >=10, stock_B <=10)
2Out [1]: array([ True, False, False, True, False], dtype=bool)
3
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.
1> #Assign values to Stock A and Stock B
2> stock_A = 10
3> stock_B = 12
4>
5> #Test if Stock A is equal to 10 or Stock B is equal to 12.
6> (stock_A==10) | (stock_B==15)
7[1] True
8
For applying the 'or' operator to Numpy arrays, we use the np.logical_or()
function.
1
2> # The following are two arrays containing returns from two stocks over the past five days.
3>
4> stock_A = np.array([10, 8, 9, 11, 12])
5> stock_B = np.array([8,11,10,10,12])
6>
7> # Stock A exceeds 10 (Inclusive) OR Stock B is below 10 (Inclusive)
8>
9> np.logical_or(stock_A >=10,stock_B <=10)
10array([ True, False, True, True, True], dtype=bool)
11>
12
not
This is called logical 'not' operator. It takes each element of the object and gives the opposite logical value.
1> #Assign values to Stock A and Stock B
2> stock_A = 10
3> stock_B = 12
4>
5> #Examples of logical not operator
6> not ((stock_A==10) | (stock_B==15))
7[1] False
8> not True
9[1] False
10> not 5>10
11[1] True
12
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.