Array in python

   Array in python   

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 

2.Create an 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


Without ALIAS

With ALIAS

Using ‘Star * ’

          Import array


      Import array as arr

      from array import *

 
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  .
  (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 



         TYPE CODE

    MEANING 

           SIZE
b
Signed char
1
B
Unsigned char 
1
h
Short signed int
2
H
Short unsigned int
2
i
Signed int
2
I(Capital i)
Unsigned   int
2
l(small L)
Long signed int
4
L
Long unsigned  int
4
f
float
4
d
double
8


Signed Int : It can hold  zero ,Positive and Negative number
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)

Array in python

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 
4.Important point to note that  negative indexing also exist in python and the 
   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

Array in python

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 

Name of Function
  append()
    Insert()
Description 
Used when we want to add a single element at the end of an array.
It will add an element at particular 
Place where user
Want . Syntax

a.insert(2,3)
First number is index number or a position where user wants to add element, where second one is value 
Array inpython

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 : 
array('i', [2, 4, 5, -9, -8, 2, 5])




To Delete an element python provide  2 functions



Name
pop()
remove()
Description
In pop() function user have to mention index number ,if not then By default it will remove the last one 
In remove() function user have to mention the value from array which has to remove 











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 size of an array5
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])