NumPy exp2()

The numpy.exp2() function calculates 2 raised to the power of each element in an input array.

Syntax

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

Parameters

ParameterTypeDescription
xarray_likeInput values. Each element will be used as an exponent to base 2.
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 the power function.
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 with values of \(2^x\) computed element-wise. If the input is a scalar, a scalar is returned.


Examples

1. Computing 2^x for a Single Value

Here, we compute \(2^x\) for a single value.

</>
Copy
import numpy as np

# Define an exponent value
x = 3

# Compute 2^x
result = np.exp2(x)

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

Output:

2^3 = 8.0