Array definition
Array is collection of elements of similar data type.
When we have to store multiple values of similar type, instead of multiple variables we can use an array.
Advantages of Arrays
Array in python is collection of similar type of data and as it is dynamic it can grow and shrink.
Array in python is dynamic so it is not required to define the size of an array initially.
Array module provides many methods that are useful to work on it.
With use of array, work on multiple elements is possible.
Creating an array in python
To create array, array module is imported. There are three ways to import array module.
Examples are array1,array2 and array3:
Array1.py
#creating array1
#syntax
'''
arrayname=array(type code,[elements])
b-> signed integer-->1
B->unsigned ineteger-->1
i->signed integer->2
I->unsigned ineteger->2
l-> signed integer->4
L->unsigned integer->4
f-> floating point->4
d->double precision floting point->8
u->unicode character->2'''
import array
a=array.array('i',[1,2,3])
print(a)
Array2.py
import array as ar
a=ar.array('i',[1,2,3,4])
for i in a:
print(i)
#creating character array'''
from array import *
c=array('c',['a','b','c'])
for i in c:
print(i)
# creating multidimensional array
from numpy import *
a1=array(['abc','bcd','cde'],dtype=str)
print("String array is")
for i in a1:
print(i)
Array3.py
from array import *
a=array('i',[1,2,3,4])
for i in a:
print(i)
Indexing and slicing on Arrays
An index represents the position number of an element in an array.
Len() function is used to find the number of elements in the array.
N=len(x) where x is an array.
A slice represents a piece of an array. In slicing operation, we get piece of array.
Slicing provides group of array elements.
The general format of slice is:
Arrayname[start: stop: stride]
Here from above any one or two can be eliminated.
They can be positive or negative integer numbers.
The stride represents step size.
Example array4.py
#indexing and slicing on array
from array import *
x= array('i',[1,2,3,4,5,6,7,8,9,10])
y=x[2:4]
print(y)
y=x[1:]
print(y)
y=x[:3]
print(y)
y=x[-4:]
print(y)
y=x[-4:-2]
print(y)
y=x[-4:-1]
print(y)
y=x[-5:-1]
print(y)
y=x[1:8:2]
print(y)
# print with range element
for i in x[3:10]:
print(i)
print("Print array with range ")
n=len(x)
for i in range(n):
print(x[i])
Output:
array('i', [3, 4])
array('i', [2, 3, 4, 5, 6, 7, 8, 9, 10])
array('i', [1, 2, 3])
array('i', [7, 8, 9, 10])
array('i', [7, 8])
array('i', [7, 8, 9])
array('i', [6, 7, 8, 9])
array('i', [2, 4, 6, 8])
4
5
6
7
8
9
10
Print array with range
1
2
3
4
5
6
7
8
9
10
Processing the array
To process array, different methods are available in array class.
Array class is available in array module.
#processing the array
from array import *
a=array('i',[1,2,3,4,5])
print("Original array ",a)
a.append(10)
a.append(20)
print("After appending 10 and 20 the array is ",a)
x=int(input("Enter no. u want to count from array"))
print("The occurance of ",x ,"is ",a.count(x))
b=array('i',[6,7,8,9])
a.extend(b)
print("The array after extending b" , a)
print("The index position of 5 in array " , a.index(5))
a.insert(0,11)
print ("Insert 11 at 0 the position in array ", a)
a.pop()
print("The array after pop function", a)
a.pop(6)
print("The array after 5 value poped ", a)
a.remove(8)
print("The array after removed value 4", a)
a.reverse()
print("The array after reverse function ",a)
lst=a.tolist()
print("The array converted to list ",lst)
Output:
('Original array ', array('i', [1, 2, 3, 4, 5]))
('After appending 10 and 20 the array is ', array('i', [1, 2, 3, 4, 5, 10, 20]))
Enter no. u want to count from array1
('The occurance of ', 1, 'is ', 1)
('The array after extending b', array('i', [1, 2, 3, 4, 5, 10, 20, 6, 7, 8, 9]))
('The index position of 5 in array ', 4)
('Insert 11 at 0 the position in array ', array('i', [11, 1, 2, 3, 4, 5, 10, 20, 6, 7, 8, 9]))
('The array after pop function', array('i', [11, 1, 2, 3, 4, 5, 10, 20, 6, 7, 8]))
('The array after 5 value poped ', array('i', [11, 1, 2, 3, 4, 5, 20, 6, 7, 8]))
('The array after removed value 4', array('i', [11, 1, 2, 3, 4, 5, 20, 6, 7]))
('The array after reverse function ', array('i', [7, 6, 20, 5, 4, 3, 2, 1, 11]))
('The array converted to list ', [7, 6, 20, 5, 4, 3, 2, 1, 11])