Stack Program in Python

Stack Program in Python with Source Code. Stacks are widely used irrespective of programming language. Today we are showing the code in python. This python project is useful for beginners and CBSE KV School Class 11 and Class 12 students computer science practical file and NIELIT O Level Programming and Problem Solving through Python (Module M3-R5).

What is Stack in Computer Science ?

A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc. A real-world stack allows operations at one end only. … At any given time, we can only access the top element of a stack.


Write a program (WAP) for stack in Python Language.

Stack Program Source Code

Stack Program in Python
Stack Program in Python

Explanation of python program code

def isEmpty(stk): # isEmpty(stk) is a function declaration
if len(stk)==0: #if length of stack equal to zero
return True #then it is true
else: #if stk stack length not equal to zero
return False #return false
def push(stk,n): #push(stk,n) n is uploaded in stack (stk)
stk.append(n) # uploading to the stack by append function
def pop(stk): #Coming out of the strike
if isEmpty(stk): # if the stack is empty
print(“UNDERFLOW CONDITION”) #then it will be written “underflow condition”
else: #if the stack is not empty
print(“Deleted element:”,stk.pop()) #then the element is deleted
def peek(stk): #function peek
return stk[-1]
def display(stk): #display of strike
if isEmpty(stk): #if stack is empty
print(“No Element Present”)
for i in range(-1,-len(stk)-1,-1):
if i==-1:
print(” “,stk[i])

while True: #shows the list so that anyone
print(” Stack operations”)
print(” 1.PUSH”)
print(” 2.POP”)
print(” 3.PEEK”)
print(” 4.DISPLAY STACK”)
print(” 5.EXIT”)
ch=int(input(” Enter the choice”))
if ch==1:
n=input(“Enter the element to PUSH”)
print(“Element pushed”)
elif ch==2:
elif ch==3:
if isEmpty(stk):
elif ch==4:
elif ch==5:

Output of Stack Program in Python

Testing of Program

The stack is first pushed by 65,25,33.

Then the stack is output 33

then pop command is given 33 is deleted
Then the output stack is 25

Testing is done.

Run the python program and enter the required data.

Source code of Program -> click here

Thanks for reading and learning about this program.

for more python programs -> click here

Hello World Program in Python

Input a welcome message and display it in Python

Display the larger / smaller number in Python.

Greatest of Three Numbers in Python using Nested if

Patterns using nested loop in Python

Program to Print Pattern in Python

Program to input the value of x and n and sum of series in Python

Python Program for Armstrong, Prefect Number, Palindrome

Program of Prime number by recursion in python

Prime Number Program in Python

Write a Program to Print Fibonacci Series in Python

LCM and GCD program in Python

Python program to count number of vowels in a string

Whether a String is Palindrome or not in Python

Bubble Sort Program in Python

Linear search in python using list

Program to read a text file in python

Python program to read a file line by line

Program to Count Vowels and Consonants in Python

Stack Program in Python

Queue Program in Python

Python Leap Year Program for Beginners

Python Program to Print Series and Addition

Binary Search Program in Python

Program to find sum of digits in python

Sum of numbers divisible by 3 and 5 in python