# NumPy exp2 – A Complete Guide

Hey everyone, have you all calculated something like 2 to the power 4 or 2 to the power 1 or something like that? For example, to calculate 2 to the power 4, we used to multiply 2 by itself 4 times. Well that was a tedious task, right? All those multiplication processes took a lot of time. Well, we can get the output within a fraction of a second without even going through the long process, sounds exciting 🙂

That’s where the Python NumPy library comes into play. In this article, we will understand how can we use the NumPy exp2 function to calculate different power of 2.

Without any further due, let’s get started.

Also read: NumPy angle – Returns the angle of a Complex argument

## What is NumPy exp2?

NumPy exp2 is a mathematical function of the NumPy library that calculates 2x, where x is the input number passed to the function. Simple by the definition! Now, let us deep dive and see how to use this function in the Python program. Let us start by going through the syntax of the function.

Also read: NumPy exp – A Complete Guide

## Syntax of NumPy exp2

```numpy.exp2(a)
```

Always pay focus to the syntax of any function of the NumPy library as it will make it easier for you to write the code. In the syntax, the input `a` can be a single number as well as a NumPy array of numbers.

## Working with NumPy exp2

Now, let us write some code to understand it better.

### NumPy exp2 with Single Number

```import numpy as np

print("2**3 is :",np.exp2(3))

print("2**7 is :",np.exp2(7))

print("2**10 is :",np.exp2(10))

print("2**(-2) is :",np.exp2(-2))
```

Output

```2**3 is : 8.0
2**7 is : 128.0
2**10 is : 1024.0
2**(-2) is : 0.25
```

In the above examples, we have passed a single number as input to the `np.exp2()` function. The output of the function is a floating point number.

See how easy it is to calculate the power of 2 using the function 🙂

### NumPy exp2 with NumPy array

Let us now pass a NumPy array as input to the function.

```import numpy as np

a = np.array((-2 , 0 , 4 ))

exp2_values = np.exp2(a)

print("Input Array :\n",a)
print("Exp2 Values for each element of the Array :\n",exp2_values)
```

Output

```Input Array :
[-2  0  4]
Exp2 Values for each element of the Array :
[ 0.25  1.   16.  ]
```

Note: The function `np.exp2()` returns a NumPy array of the same dimensions as the Input array.

In the above example, the NumPy array a is passed as an argument to the `np.exp2()` function. The exp2 values for each of the elements in the input array are calculated using the `np.exp2()` function. The output of the function is also a NumPy array which is stored in the variable exp2_values.

In the next lines, we have used the print statements to print the input array and the output array respectively.

By now, you have learned how to use the function with a single number and a NumPy array of numbers. Now, let us see how can we plot the function using the Python Matplotlib library.

## Graph of the NumPy exp2

```import numpy as np
import matplotlib.pyplot as plt

a = np.array((1 , 2 , 3 , 4 , 5))

b = np.exp2(a)

plt.plot(a , b , color = "green" , marker = "o")
plt.title("numpy.exp2()")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
```

Output

In the above code, in the first two lines, we import the NumPy and Matplotlib libraries so that we can use their functionalities.

Next, we created a variable `a` that stores the NumPy array of numbers which is passed as input to the np.exp2() function. Similarly, the variable `b` stores the output array of the np.exp2() function.

From the next lines, we have used the functions of the Matplotlib library to plot the graph of the function. Let us understand each line and its purpose.

`plt.plot()` the function is used to plot the `np.exp2()` function which takes four arguments.

• The first argument is the NumPy Array of numbers, plotted on the X-axis(Horizontal Axis).
• The second argument is the output of the `np.exp2()` function, plotted on the Y-axis(Vertical Axis).
• The third argument is the color of the plot.
• The fourth argument is the marker value which emphasizes each point with a specified marker. There are different types of markers that can be used to denote the points on the curve.
• `plt.title()` sets the value of the title for the plot. Here, the title is numpy.exp2().
• `plt.xlabel()` and `plt.ylabel()` sets the name of the Horizontal and Vertical axes respectively.
• `plt.show()` is used to display the plot.

That’s all, we are done with the examples as well as the graph of the NumPy exp2 function.

## Summary

In this article, we learned how to use the NumPy exp2 function to calculate the power of 2 values. We used the function with single numbers as well as a NumPy array of numbers. We also plotted the graph of the NumPy exp2 function.

Do check out the links given under the Reference section. Keep learning and keep exploring more topics here.

## Reference

NumPy Documentation – NumPy exp2