AppDividend
Latest Code Tutorials

# Numpy logical_and Example | np logical_and() in Python

Numpy logical_and() function calculates the result of ai AND bi, for every element ai of array1 with the corresponding element bi of array2 and returns the result in the form of an array. Both the input arrays must be of the same shape for the numpy logical_and() method to work.

### Syntax

```numpy.logical_and(arr1, arr2, out=None, where=True, dtype=None)
```

### Parameter(s)

1. arr1: Input array_like containing elements ai.
2. arr2: Input array_like containing elements bi.
3. out: (ndarray, None, or tuple of ndarray) [Optional parameter] It defines the alternate output array in which the resulting product is to be placed. This must have the same or broadcastable shape as expected output.
4. where: (array_like) [Optional parameter] Where True, these are the positions where the operator is to be applied. Where False, these are the positions to be left alone in the output array.
5. dtype : [Optional parameter] It defines the type of the returned array.

### Return Value

The element-wise logical AND result in the form of an array.

### Consider the following examples.

The following example demonstrates the use of the logical_and() method and establishes the truth table for the logical and operator.

```import numpy as np

arr1 = [0, 0, 1, 1]
arr2 = [0, 1, 0, 1]
arr3 = np.logical_and(arr1, arr2)
print(arr3)

arr4 = [False, False, True, True]
arr5 = [False, True, False, True]
arr6 = np.logical_and(arr4, arr5)
print(arr6)
```

#### Output

```[False False False  True]
[False False False  True]```

#### Example 2

The following code demonstrates the case where an array element is a complex number.

```import numpy as np

arr1 = [3+4j, 3+4j]
arr2 = [1, 0]
arr3 = np.logical_and(arr1, arr2)
print(arr3)
```

#### Output

`[ True False]`

#### Example 3

The following code demonstrates the case where an empty array is passed.

```import numpy as np

arr1 = []
arr2 = []
arr3 = np.logical_and(arr1, arr2)
print(arr3)
```

```[]
```

#### Example 4

The following example demonstrates the case where arrays of different shapes are passed.

```import numpy as np

arr1 = [0, 0, 1, 1]
arr2 = [1, 0, 1, 0, 1]
arr3 = np.logical_and(arr1, arr2)
print(arr3)
```

#### Output

```Traceback (most recent call last):
File "app.py", line 5, in <module>
arr3 = np.logical_and(arr1, arr2)
ValueError: operands could not be broadcast together with shapes (4,) (5,)```

#### Example 5

The following code demonstrates the use of the where parameter.

```import numpy as np

arr1 = [0, 0, 1, 0]
arr2 = [0, 0, 1, 0]
arr3 = np.logical_and(arr1, arr2, where=[True, False, True, False])
arr4 = np.logical_and(arr1, arr2)
print(arr3)
print(arr4)
```

#### Output

```[False  True  True  True]
[False False  True False]
```

#### Example 6

The following code demonstrates the case where the dtype is to specify the data type of the elements.

```import numpy as np

arr1 = [0, 0, 1, 1]
arr2 = [0, 0, 1, 1]
arr3 = np.logical_and(arr1, arr2, dtype=np.double)
print(arr3.dtype == np.bool)
print(arr3.dtype == np.int)```

```True
False```

#### Example 7

The following code shows the application of this method in a simple programming context.

Given a sequence of numbers, find how many numbers fall within the range x to y (both included).

```import numpy as np

n = int(input("Count: "))
numbers = []

x = int(input("x: "))
y = int(input("y: "))

for i in range(n):
numbers.append(int(input()))

new_arr = np.array(numbers)
valid_arr = np.logical_and(new_arr >= x, new_arr <= y)

count = 0
for i in valid_arr:
if(i):
count += i

print("Result: ", count)```

#### Output

```Test Case 1:
Count: 5
x: 10
y: 20
1
2
11
12
21
Result:  2

Test Case 2:
Count: 4
x: 1
y: 3
1
2
3
4
Result:  3
```

That is it for numpy logical_and() function.