CBSE Class 12 Computer Science Question 39 of 63

Data Structures in Python — Question 13

Back to all questions
13
Question

Question 13

Write add(Books) and delete(Books) methods in Python to add Books and Remove Books considering them to act as append() and pop() operations in Stack.

Solution
def add(Books):
    bookname = input("Enter the book name: ")
    Books.append(bookname)

def delete(Books):
    if Books == []:
        print("Books is empty")
    else:
        print("Deleted Book", Books.pop())
        
Books = []

add(Books)
add(Books)
add(Books)
print("Initial Books Stack:", Books)


delete(Books)
print("Books Stack after deletion:", Books)
Output
Enter the book name: Three thousand stitches
Enter the book name: Wings of fire
Enter the book name: Ignited minds
Initial Books Stack: ['Three thousand stitches', 'Wings of fire', 'Ignited minds']
Deleted Book Ignited minds
Books Stack after deletion: ['Three thousand stitches', 'Wings of fire']
Answer