Computer Science Of Engineering

Simple and practical computer science tutorials, programming guides, and engineering concepts for students and beginners.

Breaking

Thursday, April 24, 2025

Write a python program to perform the given operations on a list: i. addition ii. insertion iii. slicing

 # Initial list

my_list = [10, 20, 30, 40]

print("Original List:", my_list)


# i. Addition (adding an element to the end)

my_list.append(50)

print("After Addition:", my_list)


# ii. Insertion (insert at a specific position)

my_list.insert(2, 25)  # insert 25 at index 2

print("After Insertion at index 2:", my_list)


# iii. Slicing (get a portion of the list)

sliced_part = my_list[1:4]  # from index 1 to 3

print("Sliced Part (index 1 to 3):", sliced_part)

Output:



No comments:

Post a Comment