class Stack(object): def __init__(self, limit=10): self.stack = [] self.limit = limit # Printing the stack contents def __str__(self): return " ".join([str(i) for i in self.stack]) # Pushing an element on to the stack def push(self, data): if len(self.stack) >= self.limit: print("Stack overflowed") else: self.stack.append(data) # Popping the top-most element def pop(self): if len(self.stack) <= 0: return -1 else: return self.stack.pop() # Peeking the top-most element of the stack def peek(self): if len(self.stack) <= 0: return -1 else: return self.stack[len(self.stack) - 1] # To check if stack is empty def is_empty(self): return self.stack == [] # For checking the size of stack def size(self): return len(self.stack) if __name__ == "__main__": my_stack = Stack() for i in range(10): my_stack.push(i) print(my_stack) my_stack.pop() # popping the top element print(my_stack) my_stack.peek() # printing the top element my_stack.is_empty() my_stack.size()