0
0
NumPydata~10 mins

NumPy with Matplotlib - 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 of 5 zeros.

NumPy
import numpy as np
arr = np.[1](5)
print(arr)
Drag options to blanks, or click blank then click option'
Aempty
Bones
Cfull
Dzeros
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.ones instead of np.zeros
Using np.empty which creates uninitialized values
2fill in blank
medium

Complete the code to generate 100 linearly spaced values between 0 and 10.

NumPy
import numpy as np
x = np.[1](0, 10, 100)
print(x[:5])
Drag options to blanks, or click blank then click option'
Alinspace
Barange
Clogspace
Dgeomspace
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.arange which uses step size instead of number of points
Using np.logspace which spaces points logarithmically
3fill in blank
hard

Fix the error in the code to plot a sine wave using Matplotlib.

NumPy
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, [1])
plt.show()
Drag options to blanks, or click blank then click option'
Ax
Bnp.sin
Cy
Dnp.cos
Attempts:
3 left
💡 Hint
Common Mistakes
Plotting x against x instead of y
Passing the function np.sin instead of its evaluated values
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)
Bword
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length
Using the wrong comparison operator in the condition
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words mapped to their lengths if length is greater than 4.

NumPy
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
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.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys
Using wrong comparison operator or value