Log in from a computer to take this course

You'll need to log in from a computer to start Linear Data Structures. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store

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 Stack class.



Within stack.py, create a Stack class.

Define an __init__() method for Stack. Inside the method, set an instance property top_item equal to None.


Below __init__(), define another method peek() that returns the value of the stack’s top_item using the Node method get_value().

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?