0% found this document useful (0 votes)
22 views8 pages

Programs

The document discusses different array operations in Python like insertion, deletion, searching and updating elements. It shows code examples to demonstrate inserting and accessing elements, deleting an element, searching by index, and updating values. Array methods like insert, remove, index and using indexes to update elements are covered.

Uploaded by

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

Programs

The document discusses different array operations in Python like insertion, deletion, searching and updating elements. It shows code examples to demonstrate inserting and accessing elements, deleting an element, searching by index, and updating values. Array methods like insert, remove, index and using indexes to update elements are covered.

Uploaded by

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

14/07/2023, 19:54 programs

In [65]: values=["m",'4','5','5']
#for character array

from array import array
a = array("u",values)
print(a[0]+a[1])

m4

In [72]: values=[1,4,5,5]
#for character array

from array import array
a = array("d",values)
print(a[0]+a[1])

5.0

In [73]: values=[1,4,5,5]
#for character array

from array import array
a = array("i",values)
print(a[0]+a[1])

Strings
In [74]: a="hellow world"

In [75]: print(a[0])

In [76]: print(a[3:])

low world

In [77]: print(a[:3])

hel

In [78]: print(len(a))

12

In [79]: print(a[:])

hellow world

localhost:8890/notebooks/Downloads/programs.ipynb 1/8
14/07/2023, 19:54 programs

In [80]: print(a[3:7])

low

In [83]: print(a[-31:])

hellow world

In [85]: print(a[31])

---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[85], line 1
----> 1 print(a[31])

IndexError: string index out of range

In [86]: s='rao shahbaz'

In [87]: b="qadeeer"

In [88]: z=s+b

In [89]: print(z)

rao shahbazqadeeer

In [91]: char=s[1]
print(char)

In [92]: s[1]='b'

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[92], line 1
----> 1 s[1]='b'

TypeError: 'str' object does not support item assignment

Exception Handling

localhost:8890/notebooks/Downloads/programs.ipynb 2/8
14/07/2023, 19:54 programs

In [114]: # a program that take two value from the user and divide the first value with the second
numerator=int(input("enter numerator"))
denominator=int(input("enter denominator"))

try:
div=numerator/denominator

print(div)
except ValueError as e:
print("Invalid input")
except ZeroDivisionError as e:
print("divide by zero, you should enter value greater than 0 in denominator")

enter numerator4
enter denominator0
divide by zero, you should enter value greater than 0 in denominator

In [108]: import array as arr #importing array module



a = arr.array('b',[1,2]) #defining array of type char and size 5
print(a) # printing value at 2nd index

array('b', [1, 2])

Accessing Array Element We can access each element of an array using the index of the
element. The below code shows how

When we compile and execute the above program, it produces the following result − which
shows the element is inserted at index position 1.

Output

10 30

In [ ]: ​

In [109]: from array import *



array1 = array('i', [10,20,30,40,50])

print (array1[0])

print (array1[2])

10
30

localhost:8890/notebooks/Downloads/programs.ipynb 3/8
14/07/2023, 19:54 programs

In [115]: #Insertion Operation


#insert operation is to insert one or more data elements into an array. Based on the requ

#Here, we add a data element at the middle of the array using the python in-built insert(


from array import *

array1 = array('i', [10,20,30,40,50])

array1.insert(5,60)

for x in array1:
print(x)
###When we compile and execute the above program, it produces the following result wh

###Output

#10
#60
#20
#30
#40
#50

10
20
30
40
50
60

Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all
elements of an array.

Here, we remove a data element at the middle of the array using the python in-built
remove() method.


When we compile and execute the above program, it produces the following result
which shows the element is removed form the array.

Output
10
20
30
50

localhost:8890/notebooks/Downloads/programs.ipynb 4/8
14/07/2023, 19:54 programs

In [111]: ​
from array import *

array1 = array('i', [10,20,30,40,50])

array1.remove(40)

for x in array1:
print(x)

10
20
30
50

Search Operation
You can perform a search for an array element based on its value or its index.

Here, we search a data element using the python in-built index() method.



When we compile and execute the above program, it produces the following result
which shows the index of the element. If the value is not present in the array then th
eprogram returns an error.

Output
3

In [112]: from array import *



array1 = array('i', [10,20,30,40,50])

print (array1.index(40))

Update Operation Update operation refers to updating an existing element from the array at a
given index.

Here, we simply reassign a new value to the desired index we want to update.

When we compile and execute the above program, it produces the following result which
shows the new value at the index position 2.

Output 10 20 80 40 50

localhost:8890/notebooks/Downloads/programs.ipynb 5/8
14/07/2023, 19:54 programs

In [116]: from array import *



array1 = array('i', [10,20,30,40,50])

array1[2] = 80

for x in array1:
print(x)

10
20
80
40
50

In [126]: array1.append(5)
for x in array1:
print(x)

10
20
80
40
50
5
5

In [129]: array1.pop()
print(array1)

array('i', [10, 20, 80, 40])

https://www.educative.io/answers/what-are-type-codes-in-python
(https://www.educative.io/answers/what-are-type-codes-in-python)

Above link to find the type code for array

In [ ]: array1

Sorting an array or list


Algorithm Step 1 − Initially, an array must be created or declared with some elements. Note
that all the elements present in the array should belong to the similar data type.

Step 2 − In order to sort the elements, the first step followed is comparing the elements. One
element is compared to the rest of the elements. If the element is smaller than the other
elements, then it is placed in the first position of the array ( when the sorted array is required in
ascending order ). So, to proceed with comparison, two loops are required. First loop is used
to select an element from the array accordingly.

localhost:8890/notebooks/Downloads/programs.ipynb 6/8
14/07/2023, 19:54 programs

Step 3 − The second loop or the inner loop is used to compare the element that is selected in
the first loop with the other elements. The elements’ selection is done by incrementing the
index number. This step is one of the most important steps throughout the process of sorting.

Step 4 − After comparing the element, check whether the element selected in the outer loop is
less than the element selected in the inner loop. If the element is less than the other element,
then swap the values of the elements using a “ temp ” variable. The “ temp ” variable is used
as a bridge in order to store the values of other elements and swap the values easily. ( This is
mainly meant for sorting an array in ascending order ).

Step 5 − By incrementing the index values in the loops ( both inner loop and outer loop ),
continue and repeat the entire process until all the elements are compared and sorted. After
h i i l d h li i h i d d
In [132]: arr = [5, 9, 1, 10, 3, 8, 4, 2, 7, 6]
temp = 0
max_size = len(arr)
print("The elements of the array before sorting: ");
for i in range(0, max_size):
print(arr[i], end=" ")

print()
for i in range(0, max_size):
for j in range(i+1, len(arr)):
if(arr[i] > arr[j]):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
print("The elements of the array after sorting: ")

for i in range(0, max_size):
print(arr[i], end=" ")

The elements of the array before sorting:


5 9 1 10 3 8 4 2 7 6
The elements of the array after sorting:
1 2 3 4 5 6 7 8 9 10

Lets create an array


In [148]: arr=array('i')

In [149]: arr.insert(0,1)

In [150]: arr.append(3)

In [151]: arr

Out[151]: array('i', [1, 3])

localhost:8890/notebooks/Downloads/programs.ipynb 7/8
14/07/2023, 19:54 programs

In [152]: type(arr)

Out[152]: array.array

In [153]: len(arr)

Out[153]: 2

In [154]: for i in range(100):


arr.append(i*3+2)

In [155]: arr

Out[155]: array('i', [1, 3, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 6
2, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 101, 104, 107, 110, 113, 116, 119, 122,
125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 17
6, 179, 182, 185, 188, 191, 194, 197, 200, 203, 206, 209, 212, 215, 218, 221, 224, 227,
230, 233, 236, 239, 242, 245, 248, 251, 254, 257, 260, 263, 266, 269, 272, 275, 278, 28
1, 284, 287, 290, 293, 296, 299])

In [ ]: ​

localhost:8890/notebooks/Downloads/programs.ipynb 8/8

You might also like