0
0
NumPydata~10 mins

NumPy and scientific computing ecosystem - Interactive Code Practice

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

Complete the code to create a NumPy array from a Python list.

NumPy
import numpy as np
arr = np.[1]([1, 2, 3, 4])
print(arr)
Drag options to blanks, or click blank then click option'
Aarray
Blist
Cseries
Dmatrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' causes an AttributeError.
Using 'matrix' creates a different NumPy object, not a simple array.
2fill in blank
medium

Complete the code to calculate the mean of a NumPy array.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40])
mean_value = arr.[1]()
print(mean_value)
Drag options to blanks, or click blank then click option'
Amode
Baverage
Cmean
Dmedian
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'average' is not a NumPy array method, it causes an AttributeError.
Using 'median' calculates the middle value, not the average.
3fill in blank
hard

Fix the error in the code to create a 2D NumPy array with shape (2, 3).

NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, [1]]])
print(arr.shape)
# Fix the inner list to have equal length by adding [1]
Drag options to blanks, or click blank then click option'
A0
B6
C3
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a number larger than 3 changes the data meaning.
Leaving the lists uneven causes an array of lists, not a 2D array.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

NumPy
words = ['data', 'ai', 'science', 'ml']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dlen(words)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(words) instead of len(word) causes wrong lengths.
Using word instead of len(word) as the value.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.

NumPy
words = ['cat', 'dog', 'a', 'bird']
result = { [1]: [2] for w in words if [3] }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 2
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.lower() instead of w.upper() changes the key format.
Using len(w) > 3 excludes words of length 3.