w3resource

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

NumPy Mathematics: Exercise-5 with Solution

Write a NumPy 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]

Pictorial Presentation:

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

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to get the largest integer smaller or equal to the division of the inputs.
Next: Write a NumPy program to get the element-wise remainder of an array of division.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Getting rid of unwanted characters:

You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only.

str="+++Welcome python+++"
str.strip()

print(str)

Output:

+++Welcome python+++