Create list and perform append, pop, and slice operation.

SOLUTION.

# Creating an empty list
my_list = []

# Taking dynamic input from the user
n = int(input("Enter the number of elements you want in the list: "))

# Appending elements to the list
for i in range(n):
    element = input(f"Enter element {i+1}: ")
    my_list.append(element)

# Displaying the list after appending
print("List after appending elements:", my_list)

# Performing pop operation
if my_list:
    popped_element = my_list.pop()  # Removes the last element
    print("Popped element:", popped_element)
    print("List after pop operation:", my_list)

# Performing slice operation
start = int(input("Enter the start index for slicing: "))
end = int(input("Enter the end index for slicing: "))
sliced_list = my_list[start:end]  # Slicing the list
print("Sliced list:", sliced_list)
    

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.