Array
An array can hold one type of data type at a time.
If you have a list of items, storing the book in single variables could look like this:
book1 = “Maths”
book2 = “Science”
book3 = “English”
However, what if you want to loop through the book and find a specific one? And what if you had not 3 books, but 300?
The solution is an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.
For using an array, you have to import the array module. You can import array module in 3 ways:
1] import array
2] import array as arr
3] from array import *
Following are the some array methods.
1] Insert()
In insert it has two parameters, 1st parameter is the location of the values which get updated and 2nd parameter is the value which you want to update at a particular location.
In the above example, the 7 is updated at the position of 4.
2] Append
It adds the value at the end of the array.
3] Extend
It adds elements at the end of the array. You have to add values in [] bracket otherwise it will give the error.
4] Pop
It deletes the last element from the array.
5] Remove
It removes the value which you want to remove in the array.
6] Len
It returns the number of elements in an array.
7] Reverse
It returns the reverse element of an array.