Overview:
- The numpy.logical_and() function applies the logical AND between the elements of the NumPy array-likes and/or scalar operands and returns the result.
- For the logical AND opreation only if both the operands evaluate to True the resultant value is True. Otherwise, the resultant value is False.
- The numpy.bitwise_and() operator applies the logical AND operation on each bit pair corresponding to the elements of the two NumPy array-likes.
Truth Table:
Operand 1 | Operand 2 | Operand 1 AND Operand 2 |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example 1:
# Example Python program that performs logical AND operation import numpy # Fill the n-dimensional array # Create two ndarray instances # Fill the arrays print("Array 1:") print("Array 2:") # Apply logical and # Print the result of logical_and() |
Output:
Array 1: [[ 0 0 0] [ 0 88 38] [ 0 4 92]] Array 2: [[ 0 0 0] [ 0 26 65] [ 0 23 80]] Array 1 AND Array 2: [[False False False] [False True True] [False True True]] |
Example 2:
While creating the output ndarray, the NumPy broadcasting rules are applied as the dimensions of the two ndarray instances are different.
# Example Python program that performs logical AND operation # Create and fill ndarrays for i in range(0, 3): n3 = numpy.logical_and(n1, n2) |
Output:
Array 1: [[0 0 0] [0 1 1] [0 1 1]] Shape: (3, 3) Array 2: [[1 0 1]] Shape: (1, 3) Array 1 AND Array 2: [[False False False] [False False True] [False False True]] Shape: (3, 3) |
Example 3:
# Example Python program that performs logical AND operation import numpy a = 10 # Lists of relational expressions e3 = numpy.logical_and(e1, e2) print(e3) |
Output:
[False False True] |