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
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
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):
while True: #shows the list so that anyone
print(” Stack operations”)
print(” 4.DISPLAY STACK”)
ch=int(input(” Enter the choice”))
n=input(“Enter the element to PUSH”)
print(“INVALID CHOICE ENTERED”)
print(“THANKS FOR USING MY SERVICES”)
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