Complete the code to create a NumPy array of zeros with 5 elements.
import numpy as np arr = np.[1](5)
The np.zeros function creates an array filled with zeros, which is useful for initializing memory efficiently.
Complete the code to check the memory size (in bytes) of a NumPy array.
import numpy as np arr = np.arange(10) size_in_bytes = arr.[1]
size which returns the number of elements, not bytes.itemsize which returns bytes per element, not total.The nbytes attribute gives the total number of bytes consumed by the elements of the array, which helps understand memory usage.
Fix the error in the code to create a copy of a NumPy array to avoid modifying the original.
import numpy as np original = np.array([1, 2, 3]) copied = original.[1]()
view() which creates a shallow copy sharing the same data.clone() or duplicate().The copy() method creates a new array with its own data, so changes to it don't affect the original array.
Fill both blanks to create a memory-efficient array of integers from 0 to 9 with 8-bit data type.
import numpy as np arr = np.arange(10, dtype=[1]) size = arr.[2]
np.int64 which uses more memory than needed.size which returns number of elements, not bytes.Using np.int8 reduces memory per element to 1 byte. The nbytes attribute shows total memory used.
Fill all three blanks to create a dictionary mapping each word to its length only if length is greater than 3.
words = ['data', 'science', 'is', 'fun'] lengths = { [1]: [2] for word in words if [3] }
word.upper() as key which changes the word.len(word) < 3.This dictionary comprehension maps each word to its length len(word) only if the length is greater than 3.