Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert a NumPy array to a Python list.
NumPy
import numpy as np arr = np.array([1, 2, 3]) py_list = arr.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
to_list() which does not exist.Forgetting the parentheses after the method name.
✗ Incorrect
The tolist() method converts a NumPy array to a Python list.
2fill in blank
mediumComplete the code to convert a Python list to a NumPy array.
NumPy
import numpy as np py_list = [4, 5, 6] arr = np.[1](py_list)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
tolist() which converts arrays to lists.Using
fromlist which is not a NumPy function.✗ Incorrect
The np.array() function converts a Python list to a NumPy array.
3fill in blank
hardFix the error in the code to convert a nested Python list to a NumPy array.
NumPy
import numpy as np nested_list = [[1, 2], [3, 4]] arr = np.[1](nested_list)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
tolist() which converts arrays to lists.Using
fromlist which is not a valid NumPy function.✗ Incorrect
Use np.array() to convert nested lists to multidimensional NumPy arrays.
4fill in blank
hardFill both blanks to create a list of lengths of strings from a list using list comprehension.
NumPy
words = ['apple', 'banana', 'cherry'] lengths = [[1] for [2] in words]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
word as variable but len(word) as expression without matching names.Using
len(word) but loop variable is w (mismatch).✗ Incorrect
The list comprehension uses len(w) to get length and w as the loop variable.
5fill in blank
hardFill all three blanks to create a dictionary of word lengths for words longer than 5 characters.
NumPy
words = ['apple', 'banana', 'cherry', 'date'] length_dict = { [1]: [2] for [3] in words if len([3]) > 5 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not matching the loop variable with the condition.
✗ Incorrect
The dictionary comprehension uses word as key, len(word) as value, and loops with word.