Array stores multiple values of the same type.


A type must be defined when initializing array. This specifies what elements are contained (like integer, string and others).

The most common types and their codes are:

Type Code
i integer numbers (like 1, -5, 32)
I positive integer numbers (like 8, 6, 7)
f floating point numbers (like 3.5, 12.6, 8)
u unicode symbols (like '*', 'g', '3')

The u type code for unicode characters is deprecated since version Python 3.3. Avoid using it.


In order to initialize it the array module is used.

Empty array

Example of empty integer array:

Initial values

In this example an array with 3 floating point numbers is created.

Type Mismatch

An exception is thrown if elements don't match the type code.

Let's analyze the following code:

The only lines that throw exceptions are the commented lines (such as lines 5 and 10) due to the type mismatch.


  • At Line 5 the i code allows only integer number but 8.6 does not match that.
  • At Line 8 an array for positive numbers is declared.
  • At Line 9 an element is added to the end of the array. Since 5 matches the type code (I - positive integers) it doesn't throw exception.
  • At Line 10 an element is added - -9 which is not a positive integer. For that reason an exception would be thrown if the line is uncomented.

Similarities between array and list

Both data structure are used to store multiple values and share many similarities.

Some of them are:

  • size - accessed via the len function. Example: len(numbers).
  • value access - via indexes. Example: numbers[0].
  • inserting element - via the append function. Example: numbers.append(5).
  • index error - accessing element by index that's outside of the array bounds.

Basically list and array share the same functionalities and features.

Difference between array and list

Arrays can store values only of one type specified by the type code.

Lists can store values of different types. They are not bound to a single type.

Basically arrays are used only to guarantee that the stored values are from one type.

Let's analyze the following example:

  • At line 3 an array of integers is created (specified by the code type i). All elements are from one single type - integers.
  • Having values of different types (like in line 4) causes exception.
  • Lists can store values of different types like in line 6.