Javatpoint Logo
Javatpoint Logo

Numpy Logical _and() in Python

A substantial number of sophisticated mathematical functions are available in the Python Numpy package. With the help of these functions, big, multi-dimensional arrays and matrices may be handled in Python, and different logical and statistical operations can be carried out on them. Python's numpy library enables quick mathematical computations. This post will teach us about the numpy logical _and function, one of the logical functions available in numpy.

Numpy logical _and()

The logical AND operation can be performed in Python using the numpy logical _and function. With the help of this function, we can compute elements-wise for two lists or arrays or determine the truth value for the AND operation between two variables. When dealing with boolean values, the logical _and function can be replaced by the bitwise & operator.

NumPy's logical and function syntax

The numpy logical _and() function have the following syntax:


  • x1, x2: The input arrays between the elements of which the logic and operation must be executed are denoted by the letters x1, x2. Both arrays should have the same shape.
  • Out: It is an optional argument and is an n-dimensional array. Out has the value "None" by default. The output's storage location is specified by this argument. The length must match both x1 and x2. It will automatically return a new array containing the outcome if nothing is specified.
  • Where: It is an optional array-like parameter. A condition is mentioned in the "where" parameter.
  • y: Return Value The function produces an n-dimensional array of boolean values as its output. These boolean values were created by using the AND operation to join the elements of the x1 and x2 arrays.

Logical AND Operation Truth Table

For the AND operation to work, two boolean values must be:

x1 x2 AND Operation

Numpy logical _and examples

By using a few examples, let's first comprehend how the numpy logical _and function functions. We'll import the Numpy library first.

The results of the logical AND operation between various combinations of boolean values will then be displayed. If both values are True, the logical "and" functions will mark the output as "True."

When both x1 and x2 are false.

The logical _and function result will also be false for two "False" boolean values.



When x1 is False, and x2 is True



When both x1 and x2 are True



In between two arrays, logical _and

The logical _and function can be used to join two arrays together. The arrays could be numbers, boolean arrays, or a hybrid. Look at these two boolean arrays:


[ F F F ]

Let's pick two arrays that include both boolean and numerical values. For numerical values, zero will always be False, and all non-zero values will be True.


[ F T F T T ]

Reasonable _and under conditions

We may express conditions as an alternative to an array of values. Let's look at the array "x," which we will get via the arrange() function in them. Then, we will only print values that fall between 1 and 10 by using the logical _and.


[ 0 5 10 15 ]
[ F T F F ]

This is so because 5 is the only value from x that produces True under both conditions stated.

For more than two arrays, use Numpy logical _and.

Although the logical _and function syntax only allows for comparing two arrays, we may use it to compare several arrays simultaneously.

This is feasible due to the AND operator's commutative characteristic. Two logical "and" functions will be used. Here, we will pass an external logical _and function the logical _and function as its first argument.


[ F T F T T T ]

Reduce with Numpy logical _and along the axis.

With the help of the reduction() function, we can also determine the logical "and" value between two arrays by stating the axis parameter. For instance, let's calculate the value of a two-dimensional array using the axes 0 and 1.

If the axis = 0, then

This will carry out the logical "and" operation among the items of the columns.


[ T F ]

If axis = 1, then

This will carry out the logical "and" operation for each row. The output array will be three rows long because there are three rows here.


[ F T F ]

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA