Implement a stack using list Python Program

 

#Python Program to implement stack, Stack operation (PUSH, POP, DISPLAY)

stack=[]

choice="y"

while (choice=="y"):

    print("1.Push")

    print("2.Pop")

    print("3.Show")

    your_choice=int(input("Enter your choice"))

    if (your_choice==1):

        num=input("Input any number")

        stack.append(num)

    elif(your_choice==2):

        if (stack==[]):

            print("Stack is empty")

        else:

            print("The deleted element is:",stack.pop())

    elif (your_choice==3):

        l=len(stack)

        for i in range(l-1,-1,-1):

            print(stack[i])

    else:

        print("Wrong choice selected")

    choice=input("Do you want to continue")
        
            

 

Output:

1.Push
2.Pop
3.Show
Enter your choice1
Input any number3
Do you want to continuey
1.Push
2.Pop
3.Show
Enter your choice1
Input any number6
Do you want to continuey
1.Push
2.Pop
3.Show
Enter your choice1
Input any number67
Do you want to continuey
1.Push
2.Pop
3.Show
Enter your choice3
67
6
3
Do you want to continuey
1.Push
2.Pop
3.Show
Enter your choice2
The deleted element is: 67
Do you want to continue

By cbsepython

A complete solution for the students of class 9 to 12 having subject Information Technology (402), Computer Science (083). Explore our website for all the useful content as Topic wise notes, Solved QNA, MCQs, Projects and Quiz related to the latest syllabus.

Leave a Reply

Your email address will not be published. Required fields are marked *