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
Copywrite © 2020-2024, CBSE Python,
All Rights Reserved
error: Content is protected !!