Stacks using Python

Stacks are abstract data types that are commonly used in almost all programming languages. A stack is a data structure that simulates real-world stacks such as a deck of cards, a stack of plates, etc. In this article, I will introduce you to the concept of stacks in computing and their implementation using Python.


The stack is a data structure that looks like a list in Python where you can add and remove items. There are a few important terms you will know when implementing stacks with any programming language:

  1. Removing an item from a stack is called popping.
  2. Inserting an item into a stack is called pushing.

Stacks follow the principle of Last-in-first-out data structures, where the last item inserted is the first item out. It generally has five functions:

  1. is_empty: it returns True if the stacks are empty and return False if the stack is not empty.
  2. push: it inserts an item to the top of the stack.
  3. pop: it removes and returns the top item from the stack.
  4. peep: it returns the top item from the stack but it doesn’t remove it.
  5. size: it returns an integer that represents the number of items present in the stack.

Implementation of Stacks using Python

So, as I have introduced you to all the functions of stacks, it will now be easy for you to understand the implementation of stacks using the Python programming language. Here’s how you can implement a stack data structure using Python:

Now let’s add some items into the stacks and initialize some of the functions of a stack using Python:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


The Stacks are an important data structure in computer science that follows the principle of last in first out data structures. Some of the applications where the stack data structure is used are:

  1. language analysis without context
  2. evaluation of arithmetic expressions
  3. management of function calls
  4. traversal of trees and graph algorithms

Also, Read – All Machine Learning Algorithms Explained using Python.

So I hope you liked this article on the concept of stack data structure and its implementation using Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1538

Leave a Reply