Data Structure and Algorithm in Python - Session 3 (Array)

Опубликовано: 21 Май 2026
на канале: UnCode
7
0

What is an array?
Array in python
Basic operations in array
1. Traversal
2. Insertion
3. Deletion
4. Searching
5. Update
***********************************************************************
Notes from the class which can be sued in vscode:
What is an array?
1. As array is a special variable which can hold multiple values at a time.
2. The value/items are stored in contiguous memory locations.
3. The values/items can be access by the position of the item
i.e. index and the offset to the base value
4. starting index = 0,to access the 3rd element we will set,
the offset = 2 and add it to the index.

#Arrays in python
we need to import a module and use the below syntax
module.array(data_type, value_list)
Below are the data types acceptable in the array method
i int
I long
b int
B int
h int
H int
l int
L int
f float
d float

import array as arr

int_array = arr.array('i', [20, 30, 40, 20, 10, 50])

#Basic operation: Travlesal or Looping
print("The values of integer in the new array is as below: ")
for i in range(0,6):
print(int_array[i], end = " ")

print("\n")

print("Another way of traversal")
print("The values of integer in the new array is as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

#Basic operations: Insertion
use append() method to add an element to the end of the array
int_array.append(150)
int_array.append(250)
print("The values of integer in the array after appending two values is as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

use insert(pos, value) method to add an item in the middle of the array
int_array.insert(1,120)
print("The values of integer in the array after inserting a value in position 1 is as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

#Basic operation: Deletion
#use pop() method to remove the last element of the array. This method will remove the last
#element and return the removed element.
removed_element = int_array.pop()
print("The removed element: ", removed_element)
print("The values of integer in the array after removing an element using pop() is as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

#use pop(pos) to remove an element at the position = pos. This will return the
#removed element
removed_element = int_array.pop(3)
print("The removed element: ", removed_element)
print("The values of integer in the array after removing an element using pop(3) is as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

#use del array[index] to remove an element from the array at position= index.
del int_array[4]
print("The values of integer in the array after removing an element using del int_array(5) is as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

#use remove(val) to remove an element from the array with value = val
removed_el = int_array.remove(30)
print("The removed element: ", removed_el)
print("The values of integer in the array after removing an element using remove(30) is as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

#Basic operation: search
#use index(val) method to find the first matching postion of the element with value = val
print("The first matching postion of 20 in int_array: ", int_array.index(20))

print("The first matching postion of 50 in int_array: ", int_array.index(50))

#Basic opeartion: update
int_array[2] = 60
print("The values of integers in the array after updating the value using int_array[2] = 60 as below: ")
for value in int_array:
print(value, end = " ")

print("\n")

#Other operation
#use len(array_name) method to find the length of the array
print("The length of the int_array is : ", len(int_array))