﻿ Python NumPy: Get the powers of an array values element-wise - w3resource

Python NumPy: Get the powers of an array values element-wise

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-5 with Solution

Write a Python program to get the powers of an array values element-wise.

Note: First array elements raised to powers from second array.

Sample Solution:-

Python Code:

``````import numpy as np
x = np.arange(7)
print("Original array")
print(x)
print("First array elements raised to powers from second array, element-wise:")
print(np.power(x, 3))
```
```

Sample Output:

```Original array
[0 1 2 3 4 5 6]
First array elements raised to powers from second array, element-wise:
[  0   1   8  27  64 125 216]
```

Python Code Editor:

```import numpy as np
x = np.arange(7)
print("Original array")
print(x)
print("First array elements raised to powers from second array, element-wise:")
print(np.power(x, 3))```

Improve this sample solution and post your code through Disqus