Python Stack Example | Stack Program In Python Tutorial
In Python, we can use the List data type because we can add or remove the item from the list. A list is a built-in datatype we can use to create a Stack. The Stack is an Abstract Data Type in Python. Stack stores an Item in order in which they were added. Items can add and removed from the top of the stack. The famous principle the Stack is using called the LIFO(Last In, First Out).
Python Stack Example
Basic Python Stack Operations are following.
- Add item to the Stack.
- Remove the item from the Stack.
- How many items are there in the Stack?
Let’s take the example of Stack.
Create one file called app.py and write the following code.
# app.py class Stack: def __init__(self): self.items =  def push(self, item): self.items.append(item) dataStack = Stack() dataStack.push('krunal') print(dataStack.items)
In the above code, we have defined one list called items inside the Constructor.
Then, we have created a method called a push. The push method is responsible for adding an item to the Stack.
Next step is that we have created an object from the Stack class and called a push method to an object and pass an item in the parameter.
Now, that item will be added inside the list, and finally, we have printed that list. See the output below.
Now let’s interact with the above code and see the output. Remove the last three lines of the code.
# app.py class Stack: def __init__(self): self.items =  def push(self, item): self.items.append(item)
Okay, now go to the terminal and type the following command.
python3 -i app.py
Python Stack Pop Example
We have seen the Push Example in Stack. Now let’s see the Pop example in Stack.
Pop Example in Stack is the same as the Python List Pop method.
If we do not specify the argument that which item needs to be removed then it will by default remove the last item in the list.
Stack pop also works the same way. That is why we need to define a pop method in the Stack class and see the output.
# app.py class Stack: def __init__(self): self.items = ['Imhotep', 'Anak Su na moon'] def push(self, item): self.items.append(item) def pop(self): self.items.pop()
In the above example, we have added two items at the time of building an object means in the constructor.
Now, when we call the pop() method on the Stack object, it will remove the last item from the stack which here is Anak Su na moon. So only the Imhotep will be there in the Stack.
Let’s interact with the Python Shell and see the output.
Check Stack Items Length
We can check the total number of items of Stack or length of the Stack by len() function of Python. Let’s see the output below.
# app.py class Stack: def __init__(self): self.items = ['Imhotep', 'Anak Su na moon'] def push(self, item): self.items.append(item) def pop(self): self.items.pop() def length(self): return len(self.items)
Now, we have defined one function called length. It will return the total number of items inside the Stack.
See the output below.
Finally, Python Stack Tutorial Example | Data Structures in Python is over.