0
0
NumPydata~10 mins

np.power() and np.square() in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to square each element in the array using np.square().

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
squared = np.[1](arr)
print(squared)
Drag options to blanks, or click blank then click option'
Aabs
Bpower
Csqrt
Dsquare
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sqrt() which computes the square root instead.
Using np.abs() which computes absolute values.
2fill in blank
medium

Complete the code to raise each element in the array to the power of 3 using np.power().

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
cubed = np.power(arr, [1])
print(cubed)
Drag options to blanks, or click blank then click option'
A3
B4
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 which squares instead of cubes.
Using 0 which results in all ones.
3fill in blank
hard

Fix the error in the code to correctly square the array elements using np.power().

NumPy
import numpy as np
arr = np.array([2, 3, 4])
squared = np.power(arr, [1])
print(squared)
Drag options to blanks, or click blank then click option'
A1
Barr
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 which returns the original array.
Using 0 which returns ones.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their squared lengths as values, only for words longer than 3 characters.

NumPy
import numpy as np
words = ['data', 'science', 'ai', 'ml']
squared_lengths = {word: [1] for word in words if len(word) [2] 3}
print(squared_lengths)
Drag options to blanks, or click blank then click option'
Anp.square(len(word))
Bnp.power(len(word), 3)
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.power(len(word), 3) which cubes instead of squares.
Using <= which selects shorter words.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their squared lengths as values, only for words longer than 2 characters.

NumPy
import numpy as np
words = ['cat', 'dog', 'a', 'elephant']
squared_lengths = { [1]: [2] for word in words if len(word) [3] 2}
print(squared_lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Bnp.square(len(word))
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of upper().
Using < instead of > for filtering.
Using len(word) without np.square().