Python Programming for Beginners Part — 5
Python Arrays
Hey guys!
In last week’s article, we had a detailed discussion on the repetitive statements / loops in python. In this article, we are going to talk about python arrays, array indexing, arrays slicing, and important methods of arrays with example. The arrays provide the simplest way to store a group of values in a single array variable. Thus, pull your socks up to get nitty gritty of arrays.
What is an array?
In Python, an array is an object that stores a group of similar types of elements. Arrays offer a simple way to store multiple values in a single variable. Array stores and process group of elements easily. Array stores only one type of data that means an integer array can store only integer data elements and float array can store only float type elements. Array does not allow us to store different types of data in a single array. Python arrays are dynamic in nature. Its size is increases or decreases automatically. When an element is deleted, its size is decreased and when and elements are added its size is increased.
Advantages of using an array
· Array is like list. But array can store only similar type of elements whereas list can store different types of elements.
· Array takes less memory space than list.
· Array executes faster than list.
· In Python, array size is not fixed. Thus, no need to specify the number of elements are going to store in the array.
· Array can grow and shrink during runtime.
· Array module has various methods to deal with array.
How to create an array
As discussed earlier, array stores group of elements of same type. Thus, array element type should be specified at the time of creating array object. The array syntax is given below:
arrayname = array(typecode, [elements])
The type codes are given in the table below:
Import the array module
To use array in the python program, it is mandatory to import array module. We can import the array module in three different ways that are given below:
1. Import array
It helps us get access to an array class of array module that allows us to create an array object. For example,
2. Import array module and give an alias name.
We import array module and assign array module name to an alias name. Now, we can access array module by using arrays alias name that is demonstrated in below example.
3. Import all classes, objects and variables from array module.
We can import all classes, objects and methods of array module by using from array import * statement in python. For example
Indexing and slicing of arrays
Array elements index begins from zero to n-1. That means, index of the first element will be 0 and index of the second element will be 1 and so on and the index of last element will be (array length — 1). For example,
We can retrieve the first element of an array. For example,
It is important to note that Python allows negative indexing. Thus, -1 is the index of the last element of the array. That means, Python allows indexing of array from right hand side or from last element also. For example,
Therefore, we can retrieve the last element of array using negative index. For example,
We can also create an array from another array. For example,
Slicing of array
We can perform slicing operations on array that extract subset of elements from the array. The general syntax is given below:
array(start : stop)
The start and stop indicate the beginning and ending index of items in that array. Thus, slicing copies a portion of an array into a new array taking elements from start index to stop index (stop index element not included). For example,
To get an array containing the last n elements of the array, we can use the slicing syntax as given below:
array(-n:)
For example,
We can also use the following syntax to slice an array with step size.
array(start : stop : step)
The start and stop indicate the beginning and ending index of the array and step size indicates the jump of number of indices. The default start and end indices are 0 and (stop — 1) respectively and default step size is 1. For example,
Array Methods
In python, array module provides various methods to perform operations on array. The most common methods are given in the table below:
Each method is illustrated with the help of the example below:
Variables of Array class
Typecode
It is used to indicate the array elements type. The typecode character is used to specify elements type in the array. We can use array.typecode to get to know the elements type of the array. From example,
The itemsize
The array.itemsize reveals the memory space occupied (in bytes) by one array item in the internal memory. For example.
Conclusion
In this article, we have explored python arrays, array indexing, arrays slicing, and important methods of arrays with example. You can also access Jupyter notebook to perform tutorial shown in this article.
Next up in the series, we will discuss NumPy arrays.
On wrapping up notes, feel free to share your comments. Your comments will surely help me to present contents in a better way. See you next week.