NumPy power()

The numpy.power() function raises elements of an array to the power of corresponding elements in another array, element-wise. Both arrays must be broadcastable to the same shape.

Syntax

</>
Copy
numpy.power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1array_likeThe base values.
x2array_likeThe exponent values. If x1.shape is not equal to x2.shape, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing power.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array where each element of x1 is raised to the corresponding power in x2. If both inputs are scalars, a scalar is returned.


Examples

1. Computing Power of Scalars

Computing the power of a single base raised to a single exponent.

</>
Copy
import numpy as np

# Define base and exponent
base = 2
exponent = 3

# Compute power
result = np.power(base, exponent)

# Print result
print("2^3 =", result)

Output:

2^3 = 8