Technology3 min read

Implementing Stack in Python

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

We all know about Stack data structure, right? For those of you who do not know, Stack is a data structure which is used to store data in a structured way.

In a Stack, data which is stored at the end, is the data which is removed first. This is behavious of Stack is also known as Last in First Out or First in Last Out.

A stack data structure generally has the following 3 methods:

  1. push(): For adding data to the stack.

  2. pop(): For removing data from the stack.

  3. size(): To check the size of stack.

  4. isEmpty(): To check if the stack is empty or not.

A few other points to remember are:

  1. You cannot add or remove data randomly from a Stack.

  2. The addition and removal of data elements happen from a single side.

So let's write a simple class Stack to implement stack in python.


Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next