0
0
NumPydata~10 mins

np.std() and np.var() for spread 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 calculate the standard deviation of the array.

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
std_dev = np.[1](arr)
print(std_dev)
Drag options to blanks, or click blank then click option'
Amean
Bstd
Cvar
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.mean() instead of np.std()
Using np.var() which calculates variance, not standard deviation
2fill in blank
medium

Complete the code to calculate the variance of the array.

NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
variance = np.[1](arr)
print(variance)
Drag options to blanks, or click blank then click option'
Avar
Bstd
Cmean
Dmedian
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.std() instead of np.var()
Using np.mean() which calculates the average, not variance
3fill in blank
hard

Fix the error in the code to correctly calculate the standard deviation.

NumPy
import numpy as np
values = [2, 4, 6, 8]
result = np.std([1])
print(result)
Drag options to blanks, or click blank then click option'
Avalues.tolist()
Bvalues
Clist(values)
Dnp.array(values)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a plain Python list directly without conversion
Using list() or tolist() which do not convert to NumPy array
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length
Using less than < instead of greater than > for filtering
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 4 characters.

NumPy
words = ['tree', 'house', 'river', 'sky', 'mountain']
result = { [1]: [2] for word in words if len(word) [3] 4 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word instead of uppercase
Using less than < instead of greater than > for filtering
Using the word itself as value instead of length