Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: Create a Python class for a basic stack implementation.
Answer:

class Stack:

  def __init__(self):

    self.items = []

 

  def push(self, item):

    self.items.append(item)

 

  def pop(self):

    return self.items.pop()

 

  def is_empty(self):

    return len(self.items) == 0

Example:

stack = Stack()
stack.push(1)
stack.push(2)
print(stack.pop())  # Output: 2
Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook