An array is a data structure that can contain or hold a fixed number of elements that are of the same datatype. An array is composed of an element and an index. Index in an array is the location where the element resides. All elements have their respective indices. Index of an array always starts with 0. The following diagram illustrates the array representation.
Unlike other programming languages such as Java, C, C++ and more, arrays are not that popular in Python since there are many iterable data types in Python that are flexible and fast to use such as Python Lists. However, Python arrays are still used in certain cases. We will learn all about arrays, from what they are and when they are used in this module.
Following is the list of topics that we will cover in this Python arrays module, in case you want to jump to a specific topic.
So without any further delay, let’s get started.
The basic difference between array and lists in Python is that the lists are flexible and can hold completely arbitrary data of any datatype while arrays can only hold data of the same datatype.
Also, the array is considered useful in terms of memory efficiency but it is usually slower than lists. As mentioned above, the Python array module is not that popular to use but they do get used in certain cases such as:
In Python, the array module supports numeric arrays. So to create an array in Python we will have to import the array module. Following is the syntax to create an array:
Here typecode is what we use to define the type of the value that is going to be stored in the array. Some of the common typecodes used in the creation of arrays in Python are described in the following table.
Commonly used type codes:
|Type code||C Type||Python Type||Minimum size in bytes|
Now let’s create an array using the above-mentioned syntax.
We can access the elements of an array in Python using the respective index of the elements, as shown in the following example.
The index of the array elements start from 0, when we printed the value of array1, it displayed the first element.
Following listed are some of the basic operations supported by array module in Python.
1. Traverse: iterating between elements in an array is known as Traversing. We can easily iterate through the elements of an array using for loop as shown in the example below.
2. Insertion: Using this operation we can add one or more elements at any given index.
3. Deletion: Using this operation we can delete any element residing at the specified index. We can remove any element using the inbuilt remove() method.
4. Search: Using this operation we can search for an element using its index or its value.
In the above example, we have searched for the element using the built-in index() method. Using index(3) gave the output 2 which means that 3 is at index number 2 in the array_1. If the searched value is not present in the array then the program will return an error.
5. Update: Using this operation we can update an element at the given index.
In the above example, we have updated the already existing value at index 2 instead of adding a new element.
With this, we have come to the end of this module. The next module will cover Python classes and objects. See you there!Previous Next
Download Interview Questions asked by top MNCs in 2019?