0
0
NumPydata~10 mins

NumPy array vs Python list performance - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the NumPy library with its common alias.

NumPy
import [1] as np
Drag options to blanks, or click blank then click option'
Anumpy
Bnp
Cnum
Dnpy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'np' instead of 'numpy' in the import statement.
Using incorrect library names like 'num' or 'npy'.
2fill in blank
medium

Complete the code to create a NumPy array from a Python list.

NumPy
arr = np.[1]([1, 2, 3, 4, 5])
Drag options to blanks, or click blank then click option'
Atolist
Barray
Casarray
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' which is a Python type, not a NumPy function.
Using 'tolist' which converts arrays to lists, not the other way.
3fill in blank
hard

Fix the error in the code to measure the time taken to sum elements in a Python list.

NumPy
import time
lst = list(range(1000000))
start = time.[1]()
total = sum(lst)
end = time.time()
print(end - start)
Drag options to blanks, or click blank then click option'
Aclock
Bsleep
Ctime
Dperf_counter
Attempts:
3 left
💡 Hint
Common Mistakes
Using time.clock() which is deprecated.
Using time.sleep() which pauses execution.
4fill in blank
hard

Fill both blanks to create a NumPy array and measure the time taken to sum its elements.

NumPy
import time
arr = np.[1](range(1000000))
start = time.[2]()
total = np.sum(arr)
end = time.perf_counter()
print(end - start)
Drag options to blanks, or click blank then click option'
Aarray
Btime
Cperf_counter
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of array for NumPy arrays.
Using time.time() instead of time.perf_counter() for better precision.
5fill in blank
hard

Fill all three blanks to create a Python list, a NumPy array, and measure their sum times for comparison.

NumPy
import time
lst = list(range(1000000))
arr = np.[1](lst)
start_lst = time.[2]()
sum_lst = sum(lst)
end_lst = time.time()
start_arr = time.[3]()
sum_arr = np.sum(arr)
end_arr = time.perf_counter()
print('List sum time:', end_lst - start_lst)
print('Array sum time:', end_arr - start_arr)
Drag options to blanks, or click blank then click option'
Aarray
Btime
Cperf_counter
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of array for NumPy array creation.
Using the same timing function for both measurements.