0
0
NumPydata~10 mins

Why interop matters in NumPy - Test Your Understanding

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 from a Python list.

NumPy
import numpy as np
my_list = [1, 2, 3, 4]
arr = np.[1](my_list)
print(arr)
Drag options to blanks, or click blank then click option'
Aarray
Blist
Casarray
Dfromlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' which is not a NumPy function.
Using 'fromlist' which does not exist in NumPy.
Confusing 'asarray' which also works but is not the simplest here.
2fill in blank
medium

Complete the code to convert a NumPy array back to a Python list.

NumPy
import numpy as np
arr = np.array([5, 6, 7])
py_list = arr.[1]()
print(py_list)
Drag options to blanks, or click blank then click option'
Atolist
Btolist()
Cto_list
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses and writing 'tolist' without ().
Using 'to_list' which is not a valid method.
Trying to use the built-in 'list' function directly on the array.
3fill in blank
hard

Fix the error in the code to create a NumPy array from a list of lists.

NumPy
import numpy as np
list_of_lists = [[1, 2], [3, 4]]
arr = np.[1](list_of_lists)
print(arr.shape)
Drag options to blanks, or click blank then click option'
Aarray
Btolist
Cfromlist
Dasarray
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fromlist' which does not exist in NumPy.
Using 'tolist' which converts arrays to lists, not the other way.
Confusing 'asarray' which works but 'array' is preferred here.
4fill in blank
hard

Fill both blanks to create a NumPy array and then convert it to a Python list.

NumPy
import numpy as np
py_list = [[10, 20], [30, 40]]
arr = np.[1](py_list)
converted_list = arr.[2]()
print(converted_list)
Drag options to blanks, or click blank then click option'
Aarray
Btolist
Ctolist()
Dasarray
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'asarray' instead of 'array' for creation.
Using 'tolist' without parentheses.
Mixing up the order of conversion.
5fill in blank
hard

Fill all three blanks to create a NumPy array, convert it to a list, and then create a new array from that list.

NumPy
import numpy as np
original_list = [1, 2, 3]
arr1 = np.[1](original_list)
list_from_arr = arr1.[2]()
arr2 = np.[3](list_from_arr)
print(arr2)
Drag options to blanks, or click blank then click option'
Aarray
Btolist()
Dasarray
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'asarray' for the first blank.
Forgetting parentheses in 'tolist()'.
Using 'asarray' instead of 'array' for the last blank.