Array in python
1. array is a data structure which can hold more than one value at a time
2. array is collection of element of same data type
3. Index value of an array is all ways start from zero which means if length
of an array is n , then its index number is " n - 1 "
4. Array and list are almost similar ,But a key difference is list can have a
member/elements of differnt data types together But an array can have
only elements of same data type
5. array in python is mutable , it means we can add and delete the elements in array
To create an array in python , we have to import a module of an array
There are three ways to import a module of an array
Using a star ' * ' is most likely use method ,because it will reduce length of code
' star ' means import all function in array module
3.Declare an array :
To declare an array we have to specify two things .
To declare an array we have to specify two things .
(1) Data Type
(2) Value's
To define a data type ,user should have knowledge of Type code.
Python provide some special type code do define different data types
Python provide some special type code do define different data types
Unsigned Int: It can hold Only zero and positive number
Signed char : Its range is from -128 to 127
Unsigned char : Its range is from 0 to 255
Syntax :To print an array elements and find its length print its data type
from array import *
a = array ('i',[2,4,5,-9,-8])
print("array elements are" )
print(a)
print(array.__len__(a))
print(a.typecode)
Assessing array elements :
1.We can access an array elements using there index value
2.Indexing start from zero
3.In indexing Normal traversal is from left to right
2.Indexing start from zero
3.In indexing Normal traversal is from left to right
4.Important point to note that negative indexing also exist in python and the
traversal for negative indexing is from right to left
code:
traversal for negative indexing is from right to left
code:
from array import *
a = array ('i',[2,4,5,-9,-8])
b=a[2]
print(b)
b=a[-2]
print(b)
Output = 5, -9
We had all ready discuss that array in python is mutable , so we can add and
remove/delete the elements from it
Python provides some inbuild function to add and remove the elements
Inserting an elements :
To insert an element python have 2 inbuild function
a.insert(2,3)
First number is index number or a position where user wants to add element, where second one is value
To add more than one element at the end python provide one another function extend()
But to use this function multiple values must be written in " [] " square brackets
or compiler will through an error
e.g.
from array import *
a = array ('i',[2,4,5,-9,-8])
a.extend([2,5])
print(a)
output :
To Delete an element python provide 2 functions
Concatenation means take two or more array of same type and join them ,by default it will
concatenate row wise
from array import *
a=array('i',[1,2,3,4])
b=array('i',[5,6,7,8])
c=array('i',[]) #blank array
c=a+b
print ("Array c =",c)
Array input from user:
We can take values of an array from user
from array import *
a=array('i',[])
n=int(input("Enter size of an array"))
for i in range (n):
x=int(input("Enter values of an array"))
a.append(x)
print(a)
output :
Enter values of an array2
Enter values of an array3
Enter values of an array6
Enter values of an array8
Enter values of an array9
array('i', [2, 3, 6, 8, 9])