You have an understanding of how stacks work in theory, so now let’s see how they can be useful out in the wild — with Python!
Remember that there are three main methods that we want our stacks to have:
- Push - adds data to the “top” of the stack
- Pop - provides and removes data from the “top” of the stack
- Peek - provides data from the “top” of the stack without removing it
We also need to consider the stack’s size and tweak our methods a bit so that our stack does not “overflow”.
Let’s get started building out our
Within stack.py, create a
__init__() method for
Stack. Inside the method, set an instance property
top_item equal to
__init__(), define another method
peek() that returns the
value of the stack’s
top_item using the