Stack Operations in Python

Stack Operations in Python

Stack Operation using List in Python

CONTENT

  • PROGRAM STATEMENT
  • CONCEPT
  • SOLUTION
  • EXPLANATION
  • OUTPUT

PROGRAM STATEMENT

Write a Python program to implement the Stack Operations i.e, Push and Pop using List and demonstrate its working.

CONCEPT

Python is a simple and very powerful programming language. The reason for it to be simple is that there are a lot of built-in functions for our use which solves a lot of problems. Python uses an interpreter which means it executes each line before moving to the next one, unlike the languages which use a compiler that executes the entire program together. For demonstrating the stack operations, we are going to use a data structure called List in python.

The list is equivalent to an array, but with some added benefits.

Important Characteristics of Lists:

  • Lists are ordered.
  • Lists can contain any arbitrary objects.
  • List elements can be accessed by index.
  • Lists can be nested to arbitrary depth.
  • Lists are mutable.
  • Lists are dynamic.

Example of a simple list:

simple list

=> The list in python is created using the square brackets as shown in the example above.

=> List can also be created by using a constructor of inbuilt class List as follows:

constructor list

=> It is not necessary to store similar items in a list. Elements of the list can belong to different data types as shown below:

different element's list

The List class contains various methods which perform different operations on the list like append(), pop(), remove() etc. These operations can be viewed by using the dir() as shown below:

=> There are methods starting with double underscores which are called magic methods or dunder methods, but those are not necessary for stack operations.

=> Pay attention to the methods without the underscores. These are the built-in methods at our disposal. We will be using some of these methods in our program.

SOLUTION

# defining the push function
def push():
    item = input('Enter the element to push: ')
    # using the built-in method append()
    my_list.append(item)
    # append() places the element at the end of the list


# defining the pop function
def pop():
    # using the built-in method pop()
    my_list.pop()
    # pop() removes an element from the end of the list


# defining the display function
def display():
    print(my_list)


# creating an empty list using constructor
my_list = list()
# running an infinity loop
while True:
    print('''Stack Operations
    1. Push
    2. Pop
    3. Display
    4. Exit''')
    choice = int(input('Enter your choice: '))
    if choice == 1:
        push()
        print('Push successful.')
    elif choice == 2:
        pop()
        print('Pop successful.')
    elif choice == 3:
        display()
    elif choice == 4:
        quit()
    else:
        print('Invalid Input. Try again with a valid input.')
 

EXPLANATION

Let us understand the program in order of execution.

Firstly, we create an empty list named my_list using the constructor. The program runs an infinite loop which allows the user to continuously perform the stack operations. The menu is given to the user in the output console and the program waits for the user to enter the choice. Depending on the value of choice entered by the user, the conditional statement (if-elif-else) comes into play. It compares the values and calls an appropriate function which is already defined i.e, push(), pop() and display(), with respect to the value entered by the user. Our functions use built-in methods of the list to carry out the stack operations. The built-in list methods used in our program are:

append(element) - It inserts the element at the end of the list.

pop() - It removes the element at the end of the list.

=> You can see the functionality of other methods of the list at https://docs.python.org/3/tutorial/datastructures.html

OUTPUT

stack output

This is a Python translation of original writing in JAVA.


Top