How to define array in python?

An array is basically a data structure which can hold one value at a time. It is a collection or ordered series of elements of the same type.
Many of us gets confused whether list and arrays are the same or not.
Lists vs. arrays
- Arrays and lists have the same way of storing data.
- Arrays take only a single data type elements but lists can have any type of data.
- Other than a few operations, the kind of operations performed on them are different.
Array in python can be created after importing the array module.
e.g.,
import array arr=array.array('i', [1,2,3]) print(arr)

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.
Subscribe
Login
Please login to comment
0 Discussion