Simply an array is a group of related data items that share a common name. Arrays are of sequence types and behave very much like lists. The major difference is the elements of a list can be of different types while only the elements of same time construct an array. These element may be integers, floating point numbers or characters.

Arrays are so useful in mathematical operations. They can speeds up mathematical manipulations by several orders of magnitude.

In python, arrays are not a core data type like integers, floats or strings. There is a built-in standard module named ‘array’. In order to have access to the array type, the module ‘array’ must be imported. So, when we need to use array, we must import it-

from array import *

Array constructor

We have already known that arrays can hold data of same type. The type is specified by using a type code at the time when an array object is created. The constructor of an array is as follows-

If you look at the example given above, you will see we have added new items to array using several methods. First method used here is insert() method. This method is used to insert a new element into a specified position. Thus it takes two arguments: position and then element. Next method used is append(), which takes only one argument. By using this method an element is added at the end of an array. If you wish to add several items from another array, then you can use extend method. It is also possible to add items from a list. fromlist() method adds all the items of that list into array.

Remove items from array

If you wish to remove an item from existing array, then you can use either the method remove() or pop(). Thie method remove() takes only one argument, the item to be removed. On the other hand, pop() also takes one argument, but it is optional. Then the argument is not supplied, it will remove the last element.

Some other commonly used methods include index(), reverse(), tostring() and so on. The method index() is used to return the index of an element. Another method used is reverse(), which reverses the elements of an array. The method tostring() returns the array converted to string.

You can also get access of array by using Numeric module. You may probably feel more comfort using Numeric module rather than previous one. If you read this section, then you will find the reason behind this.

Here you don’t need to specify type code at all! Here the result is shown with decimal points because one of the elements of the array contain decimal point(3.5). As a result python automatically converts all the elements to floating point numbers at the time of storing this valuses. The reason behind this is already known to us, array can only hold data of same type.

Note:The elements of an array should be either integer or floating point number.

Creating an empty array

You can also create an empty array by using the function zeros(). This function takes two parameters; number of elements and the type. Type is specified by either ‘Int’ in case of integer or ‘Float’ in case of floats.

zeros() function can be handy in mathematical manipulations. Let’s consider an example. Suppose you have to record the temperature of an object at an interval of one minute from the first minute. You can do that as follows-

Nallo, I have provided a link below to part of the Python documentation that explains arrays. Essentially, an array is a way to force each element to be a specify basic type (like all integers) where lists allow you to put in an integer then a string then an object etc. It forces a structure which is homogeneous. Because of specifying a basic type and which is the same throughout, it can store values more compactly in memory.

Most people I know really don't need such a structure unless you are limited on memory and want to do some fast numerical calculations on a set of data which you know is of the same type.

In your explanation of array-use in Python, you had one screenshot where it said

from Numeric import *

. I tried this and I got the "ImportError" message. Can you tell me why it does this, or provide an alternative to this. I need to use arrays for my assignment and I can do it, but I need the methods from the imports. Just to clarify, I just need the Module names. Thank you!