0
0
NumPydata~10 mins

np.power() and np.square() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.power() and np.square()
Start with input array
Choose function: np.power or np.square
np.power: raise each element to given exponent
np.square: raise each element to power 2
Output: new array with powered values
End
The flow starts with an input array, then applies either np.power with a chosen exponent or np.square which squares each element, producing a new array as output.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
pow_arr = np.power(arr, 3)
sq_arr = np.square(arr)
This code creates an array, raises each element to the power 3 using np.power, and squares each element using np.square.
Execution Table
StepInput ArrayFunctionExponentOutput ArrayExplanation
1[1, 2, 3, 4]np.power3[1, 8, 27, 64]Each element raised to power 3
2[1, 2, 3, 4]np.square2 (fixed)[1, 4, 9, 16]Each element squared (power 2)
3----End of operations
💡 All elements processed; output arrays created for both functions.
Variable Tracker
VariableStartAfter np.powerAfter np.square
arr[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
pow_arrN/A[1, 8, 27, 64][1, 8, 27, 64]
sq_arrN/AN/A[1, 4, 9, 16]
Key Moments - 3 Insights
Why does np.square not need an exponent argument?
np.square always raises elements to the power 2 internally, so you don't provide an exponent. See execution_table step 2 where exponent is fixed at 2.
Can np.power handle exponents other than 2?
Yes, np.power lets you specify any exponent, like 3 in step 1 of execution_table, unlike np.square which is fixed to 2.
Are the original array values changed after these operations?
No, the original array 'arr' stays the same as shown in variable_tracker; new arrays are created for results.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 1, what is the output of np.power(arr, 3)?
A[1, 8, 27, 64]
B[1, 4, 9, 16]
C[3, 6, 9, 12]
D[1, 2, 3, 4]
💡 Hint
Check the Output Array column in step 1 of execution_table.
At which step does the exponent get fixed to 2 automatically?
AStep 1
BStep 2
CStep 3
DNone
💡 Hint
Look at the Exponent column in execution_table for step 2.
If we change np.power(arr, 3) to np.power(arr, 2), what will happen to pow_arr?
Apow_arr will be [1, 8, 27, 64]
Bpow_arr will be unchanged
Cpow_arr will be [1, 4, 9, 16]
Dpow_arr will be [1, 2, 3, 4]
💡 Hint
Raising elements to power 2 squares them, matching np.square output in variable_tracker.
Concept Snapshot
np.power(array, exponent) raises each element of array to the given exponent.
np.square(array) is a shortcut to square each element (power 2).
Both return new arrays; original array stays unchanged.
Use np.power for any exponent, np.square for quick squares.
Full Transcript
We start with an input array. We can use np.power to raise each element to any exponent, like 3. Alternatively, np.square raises each element to power 2 automatically. Both functions create new arrays with the results, leaving the original array unchanged. The execution table shows step-by-step how inputs transform to outputs. Variable tracking confirms original data stays the same while new arrays hold powered values. Key moments clarify why np.square doesn't need an exponent and how np.power is more flexible. The quiz tests understanding of outputs and function behavior.