python in Concept of Array and its operations in hindi by unitdiploma
•Concept of Array and its operations
•Handling Strings and Characters
Array
•An array is an object that stores a group of elements (or values) of same datatype.
•The size of the array is not fixed in python. Hence, we need not specify how many elements we are going to store into an array in the beginning.
•Arrays can grow or shrink in memory dynamically (during runtime).
•Creating an Array:
arrayname=array(type code, [elements])
a=array(‘i’, [4,6,2]) -integer type array
arr= array(‘d’,[1.5,-2.2,3,5.75]) -double type array
Importing the Array Module
•import array
a= array.array(‘i’,[4,6,2,9])
•Import array as ar
a=ar.array(‘i’,[4,6,2,9])
•from array import*
a=array(‘i’,[4,6,2,9])
(* symbol represents all)
Example 1
Output
Example 2
Output
Indexing & Slicing on Array
•An index represents the position number of an element in an array. For example, the following integer type array:
X=array(‘i’, [10, 20, 30, 40, 50])
Allocates 5 blocks of memory each of 2 bytes of size and stores the elements 10, 20, 30, 40, 50.
Example 1
Output
The len(a) function returns the number of elements in the array ‘a’ into n.
Output
Type Codes to Create Array
Change or add elements in the array
Example
Output
We can add one item to a list using append() method or add several items using extend() method.
Example
Output
Insert at particular location
Example
Output
Remove/delete element
We can delete one or more items from an array using Python’s del statement.
Example
Output
We can use the remove() method to remove the given item, and pop() method to remove an item at the given index.
Example
Output
Example: Program to store students marks in an array and find total marks and percentage of marks
Output
Example: To Search for any element in an array
Output
Problems
1.WAP to store student’s marks (60,70,75,45 and 50) into an array and find total marks and percentage of marks.
2.Take an array of elements 5,10,15,20,25,30,35,40,45,50
and perform the following operations:
a)Print the 3rd and 5th element.
b)Slice the array into two arrays from 0 to 3 and 5 onwards.
c)Change the element of 4th position by element 32
d)Delete the 6th element
e)Insert a new element 55 at last position of the array.
f)Delete the element 40 from the array.
g)Extend the array by elements 60,65,70 and 75.
h)Insert a new element 18 at 4th position of the array.
3.Program to search for the position of an element in an array.