Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add text at position (2, 3) on the plot.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [1, 4, 9]) ax.text(2, 3, [1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the text inside quotes.
Passing numbers instead of a string as the text.
✗ Incorrect
The ax.text function requires the text to be a string, so it must be in quotes.
2fill in blank
mediumComplete the code to place the text 'Peak' at the highest point on the plot.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() x = [1, 2, 3] y = [1, 4, 9] ax.plot(x, y) ax.text(x[[1]], y[[1]], "Peak") plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 3 which is out of range.
Using index 1 which is not the highest point.
✗ Incorrect
The highest point is at index 2 (the third element) in the lists x and y.
3fill in blank
hardFix the error in the code to correctly place the text 'Center' at (0.5, 0.5).
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.text([1], 0.5, 'Center') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for the coordinate.
Using a comma instead of a dot for decimal.
✗ Incorrect
The x coordinate must be a number, not a string or invalid syntax.
4fill in blank
hardFill both blanks to add text 'Max' at the maximum y value and align it to the right.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() x = [1, 2, 3, 4] y = [2, 5, 1, 7] ax.plot(x, y) max_index = y.index(max(y)) ax.text(x[[1]], y[[2]], 'Max', ha='right') plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indexes for x and y coordinates.
Using a fixed number instead of max_index.
✗ Incorrect
Both blanks should use max_index to get the x and y coordinates of the max point.
5fill in blank
hardFill all three blanks to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.
Matplotlib
words = ['cat', 'elephant', 'dog', 'horse'] lengths = { [1]: [2] for [1] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' instead of 'word' in the comprehension.
Not using len(word) for the value.
Using different variable names inconsistently.
✗ Incorrect
The dictionary comprehension uses 'word' as key, 'len(word)' as value, and filters words with length > 3.