Python Array.

  • Python does not have built in arrays as type.
  • Instead, Python has several other data types like lists and tuples that are often used as arrays but, items stored in these types of sequences need not be of the same type.

To create an array in Python, import the array module and use its array() function. We can create an array of three basic types namely integer, float and Unicode characters using this function.
The array() function accepts typecode and initializer as a parameter value and returns an object of array class.

Example

import array as ar

# creating an array with integer type
x = ar.array('i', [1, 2, 3])
print (type(x), x)

# creating an array with char type
y = arr.array('u', 'BAT')
print (type(y), y)

# creating an array with float type
a = arr.array('d', [4.1, 4.2, 5.3])
print (type(a), a)