0% found this document useful (0 votes)
17 views2 pages

Basic Operations in Arrays Using Python

The document provides Python functions for basic array operations including traversal, insertion, deletion, searching, and updating elements. It demonstrates these operations with a sample array and shows how to handle invalid indices and element presence. The main program creates an array and performs various operations while displaying the results.
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)
17 views2 pages

Basic Operations in Arrays Using Python

The document provides Python functions for basic array operations including traversal, insertion, deletion, searching, and updating elements. It demonstrates these operations with a sample array and shows how to handle invalid indices and element presence. The main program creates an array and performs various operations while displaying the results.
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/ 2

# Basic Operations in Arrays using Python

def traverse(arr):

print("Array Elements:")

for i in arr:

print(i, end=" ")

print("\n")

def insert_element(arr, index, value):

if index < 0 or index > len(arr):

print("Invalid index for insertion!")

else:

arr.insert(index, value)

print(f"Inserted {value} at index {index}")

def delete_element(arr, value):

if value in arr:

arr.remove(value)

print(f"Deleted {value} from array")

else:

print(f"{value} not found in array")

def search_element(arr, value):

if value in arr:

print(f"{value} found at index {arr.index(value)}")

else:

print(f"{value} not found in array")


def update_element(arr, index, new_value):

if index < 0 or index >= len(arr):

print("Invalid index for update!")

else:

old_value = arr[index]

arr[index] = new_value

print(f"Updated index {index} from {old_value} to {new_value}")

# Main Program

arr = [10, 20, 30, 40, 50]

print(" Array created :")

traverse(arr)

insert_element(arr, 2, 25)

traverse(arr)

delete_element(arr, 40)

traverse(arr)

search_element(arr, 30)

search_element(arr, 100)

update_element(arr, 1, 22)

traverse(arr)

You might also like