Python Array Creation

In this tutorial, we will learn about how to create an array in python along with the working examples of array creation.

Array Creation

We have already discussed that arrays can hold data of same type. The type should be specified by using a type code at the time of creating the array object as:

CopiedCopy Code
arrayname = array(type code, [elements]) 

The type code 'i', represents integer type array where we can store integer numbers. If the type code is 'f' then it represents float type array where we can store numbers with decimal point. The important type codes are given in Table

Python Array Creation

A signed integer is one with either a plus or minus sign in front. That is it can be either positive or negative. An unsigned integer is assumed to be positive.

We will take an example to understand how to create an integer type array.

We should first write the module name 'array ' and then the type code we can use is 'i ' for integer type array. After that the elements should be written inside the square braces [ ] as,

a = array('i', [4, 6, 2, 9])

This is creating an array whose name is 'a' with integer type elements 4, 6, 2 and 9.

Similarly, to create a float type array, we can write:

arr = array('d', [1.5, -2.2, 3, 5.75])

The type code is 'd' which represents double type elements each taking 8 bytes memory.