What is an array in Python?
a) A sequence of strings
b) A collection of items stored at contiguous memory locations
c) A function that returns multiple values
d) A variable that can store multiple data types
Answer:
b) A collection of items stored at contiguous memory locations
Explanation:
An array in Python is a collection of items stored at contiguous memory locations. Arrays are used to store multiple items of the same type in a single variable, allowing for efficient storage and manipulation of data.
from array import array
numbers = array('i', [1, 2, 3, 4, 5])
print(numbers) # Outputs array('i', [1, 2, 3, 4, 5])
Arrays are particularly useful when you need to work with large amounts of data that need to be processed efficiently. Unlike lists, arrays in Python require that all elements be of the same data type, which makes them more efficient in terms of memory usage and performance.
Understanding arrays is important for tasks that require efficient data storage and manipulation, such as numerical computations and data analysis.