AppDividend
Latest Code Tutorials

Python Stack Tutorial Example | Data Structures in Python

0

Python Stack Tutorial Example | Data Structures in Python is today’s topic. 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).

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.

Python Stack Tutorial Example

Content Overview

Basic Python Stack Operations are following.

  1. Add item to the Stack.
  2. Remove the item from the Stack.
  3. 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.

 

Python Stack Tutorial Example

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

 

Stack Data Structures in Python

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 same as 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.

 

Python Stack Pop Example

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.

 

Check Stack Items Length

Finally, Python Stack Tutorial Example | Data Structures in Python is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.