NumPy logical_and() function

The logical_and() function applies logical AND operation between the elements of the two numpy array-likes or scalars or a combination of them and returns the results.

Logical Operations Updated 14 Oct 2024 2 min read

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 1Operand 2Operand 1 AND Operand 2
000
010
100
111

Example 1

python
# Example Python program that performs logical AND operation

            # between two numpy ndarrays and printing the results

import numpy

            from random import randrange

# Fill the n-dimensional array

            def fillNDArray(array_in):

                for i in range(0, 3):

                    for j in range(0, 3):

                        if i == 0 or j == 0:

                            array_in[i][j] = 0

                        else:

                            array_in[i][j] = randrange(100)

# Create two ndarray instances

            n1 = numpy.ndarray(shape = (3, 3), dtype = numpy.int32)

            n2 = numpy.ndarray(shape = (3, 3), dtype = numpy.int32)

# Fill the arrays

            fillNDArray(n1)

            fillNDArray(n2)

print("Array 1:")

            print(n1)

print("Array 2:")

            print(n2)

# Apply logical and

            n3 = numpy.logical_and(n1, n2)

# Print the result of logical_and()

            print("Array 1 AND Array 2:")

            print(n3)

Output

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.

Output
# Example Python program that performs logical AND operation

            # between two ndarrays containing Boolean values

            import numpy

# Create and fill ndarrays

            n1 = numpy.ndarray(shape = (3, 3),

                               dtype = numpy.int32)

            n2 = numpy.ndarray(shape = (1, 3),

                               dtype = numpy.int32)

            n2[0][0] = True

            n2[0][1] = False

            n2[0][2] = True

for i in range(0, 3):

                for j in range(0,3):

                    n1[i][j] = numpy.logical_and(i,j)

n3 = numpy.logical_and(n1, n2)

            print("Array 1:")

            print(n1)

            print("Shape:")

            print(n1.shape)

            print("Array 2:")

            print(n2)

            print("Shape:")

            print(n2.shape)

            print("Array 1 AND Array 2:")

            print(n3)

            print("Shape:")

            print(n3.shape)

Output

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

python
# Example Python program that performs logical AND operation

            # between the elements of two array-likes containing relational expressions

import numpy

a = 10

            b = -2

# Lists of relational expressions

            e1 = [a < b,  a == b, a > b]

            e2 = [a < b,  a == b, a > b]

e3 = numpy.logical_and(e1, e2)

print(e3)

Output

Output
[False False  True]

##