Overview:
- An array type in any programming language offers a continuous storage container for homogenous types.
- The class array from the array module of Python implements an array type for basic types such as signed and unsigned integers of various lengths (8, 16, 32, 64 and 128 bits), floating point and double precision numbers.
- Python raises a type error when an attempt is made to store heterogeneous types in an array.
Example:
# An example Python program the creates an array of integers # using the Python module array import array
# Create an array of integers using the type code 'i' intArray = array.array('i');
# Insert elements into the Python array for i in range(10, 0, -1): intArray.insert(0, i);
print("Contents of the array:"); print(intArray);
print("Address and length of the array:"); print(intArray.buffer_info());
print("Size of an element in the array:"); print(intArray.itemsize);
print("The total memory occupied by the array:"); print(intArray.buffer_info()[1]*intArray.itemsize); |
Output:
Contents of the array: array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) Address and length of the array: (4378146736, 10) Size of an element in the array: 4 The total memory occupied by the array: 40 |