0% found this document useful (0 votes)
8 views4 pages

Practical 1

Uploaded by

yjgaming131121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Practical 1

Uploaded by

yjgaming131121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical 1: Array/List

Q1: Create Arrays of different data types in python using array module

import array as arr

#integer array

a=arr.array('i',[3,5,8,1,9])

print('a: ',a)

#traversing using range & array index

for i in range(len(a)):

print(a[i])

#double array

l2=[1.5,3.7,9.8] #list created

b=arr.array('d',l2)

print('\nb: ',b)

#traversing

for x in b:

print(x)

#character/Unicode array

c=arr.array('u',"Hello")

print("\nc: ",c)

#traversing

for x in c:

print(x,end=' ')

Output

a: array('i', [3, 5, 8, 1, 9])

5
8

b: array('d', [1.5, 3.7, 9.8])

1.5

3.7

9.8

c: array('u', 'Hello')

Hello

#Practical 1: Array/List

#Q 2: Implement different possible operations on Array in python using array module

import array as arr

#integer array

a=arr.array('i',[3,5,8,1,9])

print('a: ',a)

#traversing : Visiting all the elements of array one by one

for i in range(len(a)):

print(a[i])

#Accessing Individual element

print("\nElement at index 2 is: ",a[2])


#Inserting element in to the array [using insert method]

a.insert(3,60) #inserting element 60 at position/index 3

print("array after insertion of 60 at pos 3 : ",a)

#append() method can be used to insert element at end

a.append(90)

print("array after appending 90: ",a)

#Removing any element/Deletion of particular element from array

a.remove(8) #remove method can be used to remove particular element from array

print('Array after removal of 8 : ',a)

del a[2] #del can be used to delete element at particular index

print("Array after deletion of element at index 2: ",a)

#Pop() method can be used to delete the last element

#Pop(index) method can be used to delete the element at given index

a.pop()

print("Array after calling pop(): ",a)

#Search

#index(ele) method can be used to search the given ele if it is present then it will return the index or
it will generate error

print("\nElement 1 present at index: ",a.index(1))

Output:

a: array('i', [3, 5, 8, 1, 9])

9
Element at index 2 is: 8

array after insertion of 60 at pos 3 : array('i', [3, 5, 8, 60, 1, 9])

array after appending 90: array('i', [3, 5, 8, 60, 1, 9, 90])

Array after removal of 8 : array('i', [3, 5, 60, 1, 9, 90])

Array after deletion of element at index 2: array('i', [3, 5, 1, 9, 90])

Array after calling pop(): array('i', [3, 5, 1, 9])

Element 1 present at index: 2

You might also like