0
0
NumPydata~5 mins

np.power() and np.square() in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.power() do in NumPy?

np.power() raises each element of an array to the power of a given exponent. It works element-wise, meaning it applies the power operation to each number in the array separately.

Click to reveal answer
beginner
How is np.square() different from np.power()?

np.square() is a shortcut to square each element of an array (raise to the power of 2). It is equivalent to np.power(array, 2) but simpler and faster for squaring.

Click to reveal answer
beginner
Example: What is the result of np.power([2, 3, 4], 3)?

The result is [8, 27, 64] because each element is raised to the power 3: 2³=8, 3³=27, 4³=64.

Click to reveal answer
intermediate
Why might you choose np.square() over np.power() when squaring numbers?

np.square() is more readable and can be faster because it is optimized specifically for squaring. It makes your code clearer when you only want to square values.

Click to reveal answer
intermediate
Can np.power() handle fractional exponents? Give an example.

Yes, np.power() can handle fractional exponents. For example, np.power([4, 9, 16], 0.5) returns [2., 3., 4.], which are the square roots of the numbers.

Click to reveal answer
What does np.square(array) do?
AMultiplies each element by 2
BRaises each element to the power of 3
CCalculates the square root of each element
DRaises each element to the power of 2
Which function can raise array elements to any power, including fractional powers?
Anp.square()
Bnp.power()
Cnp.sqrt()
Dnp.exp()
What is the output of np.power([1, 2, 3], 0)?
A[1, 1, 1]
B[1, 2, 3]
C[0, 0, 0]
D[1, 4, 9]
Which is faster and more readable for squaring an array: np.power(array, 2) or np.square(array)?
Anp.power(array, 2)
BBoth are equally fast
Cnp.square(array)
DNeither works for squaring
What will np.power([8, 27, 64], 1/3) return?
A[2, 3, 4]
B[64, 27, 8]
C[512, 19683, 262144]
D[4, 9, 16]
Explain how np.power() and np.square() work and when to use each.
Think about general vs specific use cases for exponentiation.
You got /4 concepts.
    Give an example of using np.power() with a fractional exponent and explain the result.
    Fractional powers relate to roots of numbers.
    You got /3 concepts.