Complete the code to import the NumPy library with its common alias.
import [1] as np
NumPy is imported using the name numpy and commonly aliased as np.
Complete the code to create a NumPy array from a Python list.
arr = np.[1]([1, 2, 3, 4, 5])
The np.array() function creates a NumPy array from a Python list.
Fix the error in the code to measure the time taken to sum elements in a Python list.
import time lst = list(range(1000000)) start = time.[1]() total = sum(lst) end = time.time() print(end - start)
time.clock() which is deprecated.time.sleep() which pauses execution.The time.time() function returns the current time in seconds, suitable for measuring elapsed time.
Fill both blanks to create a NumPy array and measure the time taken to sum its elements.
import time arr = np.[1](range(1000000)) start = time.[2]() total = np.sum(arr) end = time.perf_counter() print(end - start)
list instead of array for NumPy arrays.time.time() instead of time.perf_counter() for better precision.Use np.array() to create the array and time.perf_counter() for precise timing.
Fill all three blanks to create a Python list, a NumPy array, and measure their sum times for comparison.
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)
list instead of array for NumPy array creation.Create the NumPy array with np.array(), use time.time() for list timing, and time.perf_counter() for array timing for better precision.