0
0
NumPydata~10 mins

np.mean() for average 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 average of the list using numpy.

NumPy
import numpy as np
numbers = [10, 20, 30, 40, 50]
average = np.[1](numbers)
print(average)
Drag options to blanks, or click blank then click option'
Amedian
Bsum
Cmean
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sum() instead of np.mean() which sums the numbers instead of averaging.
Using np.max() which finds the largest number, not the average.
2fill in blank
medium

Complete the code to calculate the average of the numpy array.

NumPy
import numpy as np
arr = np.array([5, 15, 25, 35, 45])
avg = np.[1](arr)
print(avg)
Drag options to blanks, or click blank then click option'
Amean
Bsum
Cmin
Dstd
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sum() which adds all elements instead of averaging.
Using np.min() which finds the smallest element.
3fill in blank
hard

Fix the error in the code to correctly calculate the average of the list.

NumPy
import numpy as np
values = [2, 4, 6, 8, 10]
average = np.[1](values)
print(average)
Drag options to blanks, or click blank then click option'
Amean
Baverage
Cavg
Dmedian
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'average' or 'avg' which are not numpy functions.
Using 'median' which calculates the middle value, not the average.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only include words longer than 3 letters.

NumPy
words = ['cat', 'house', '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)
Bword
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<=' instead of '>' which includes shorter words.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, including only words longer than 4 letters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for w in words if len(w) [3] 4 }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word instead of uppercase for keys.
Using '<' or '<=' instead of '>' which changes the filter condition.