0
0
NumPydata~10 mins

Scalar operations on arrays 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 multiply each element in the array by 3.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
result = arr [1] 3
print(result)
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * will add 3 to each element, not multiply.
Using / will divide each element by 3, which is not what we want.
2fill in blank
medium

Complete the code to subtract 5 from each element in the array.

NumPy
import numpy as np
arr = np.array([10, 15, 20, 25])
result = arr [1] 5
print(result)
Drag options to blanks, or click blank then click option'
A*
B/
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using + will add 5 instead of subtracting.
Using * will multiply each element by 5, which is incorrect here.
3fill in blank
hard

Fix the error in the code to divide each element by 2 correctly.

NumPy
import numpy as np
arr = np.array([8, 16, 24, 32])
result = arr [1] 2
print(result)
Drag options to blanks, or click blank then click option'
A*
B/
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using * will multiply instead of dividing.
Using + or - will not perform division.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths squared if length is greater than 3.

NumPy
words = ['data', 'science', 'ai', 'ml']
lengths = {word: len(word)[1] 2 for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
A**
B%
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > will filter wrong words.
Using % instead of ** will cause wrong calculations.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths if length is less than 5.

NumPy
words = ['data', 'science', 'ai', 'ml']
result = {word[1]: [2] for word in words if len(word) [3] 5}
print(result)
Drag options to blanks, or click blank then click option'
A.upper()
Blen(word)
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < will filter wrong words.
Forgetting to convert words to uppercase.