Completing methods in a LIFO data structure class

Report a typo

Assume you are given an incomplete Python class for a basic data structure that operates on a "last in, first out" (LIFO) principle. This class can add items, remove items, check the most recent item, and verify whether it's empty or not. However, some parts of the method definitions in the class are missing. Your task is to fill in the blanks in the Python class to complete the methods for pushing to the stack, popping from the stack, peeking the top item of the stack, and checking if the stack is empty.

Fill in the gaps with the relevant elements
class Stack:
    def __init__(self):
        self.stack = []
    def (self, item):
        self.stack.append(item)
    def (self):
        return self.stack.pop()
    def (self):
        return self.stack[-1]
    def (self):
        return not self.stack

s = Stack()
s.push('Hello, ')
s.push('World!')
print(s.pop() + s.pop())
toppushis_emptypop
___

Create a free account to access the full topic