A Python Array is a collection of a common type of data structures having elements with the same data type. It is used to store collections of data. In Python programming, arrays are handled by the “array” module. If you create arrays using the array module, elements of the array must be of the same numeric type.

Syntax to Create an Array in Python

You can declare an array in Python while initializing it using the following syntax.
arrayName = array.array(type code for data type, [array,items])

  1. Identifier: specify a name like usually, you do for variables
  2. Module: Python has a special module for creating array in Python, called “array” – you must import it before using it
  3. Method: the array module has a method for initializing the array. It takes two arguments, type code, and elements.
  4. Type Code: specify the data type using the type codes available (see list below)
  5. Elements: specify the array elements within the square brackets, for example [130,450,103]

Example

import array as myarray
abc = myarray.array('d', [2.5, 4.9, 6.7])