An array is basically a data structure which can hold more than one value at a time. It is a collection or ordered series of elements of the same type.
Arrays can only take a single datatype element.
Array in python can be created after importing the array module.
import array arr=array.array('i', [1,2,3]) print(arr)
a=int(input("Number of elements in the array:-")) n=list(map(int, input("elements of array:-").strip().split())) print(n)
Here we can see in the above example that we have used the map function to take the input of the array from the user.
Basic operations on array:-
- Finding the length of array- Length of an array is the number of elements that are actually present in an array. We can use the function len() to achieve it.
- Adding an element in array- Functions used to add an element to an array are append(), insert() and extend().
- Removing an element- Functions used to remove or delete an element of an array are pop() and remove()
- Array concatenation- Array concatenation is done by using ‘+’ sign as we do in strings.
This is all about defining an array and basic operations on it.