# Implementation of the Stack ADT using a Python list. class Stack : """ A stack class implemented using Python list. The end of the list is the 'top' of the stack""" def __init__( self ): """ Create the stack""" self._the_items = list() def is_empty( self ): """ Check if empty """ return len( self ) == 0 def __len__ ( self ): """ Return the length """ return len( self._the_items ) def peek( self ): """ Return the top item of the stack""" assert not self.is_empty(), "Cannot peek at an empty stack" return self._the_items[-1] def pop( self ): """ Return and remove the top element of the stack""" assert not self.is_empty(), "Cannot pop from an empty stack" return self._the_items.pop() def push( self, item ): """ Add a new item to the top of the stack""" self._the_items.append( item )