Computer Science (083)
XII (2025-26)
Practical Assignment #2
( Based on Stacks in Python )
Q1. Write a Python program to display reverse of an input string using stack.
For Example: If input string is ‘Hello’
Output should be olleH
Q2. Write a Python program to input a list of n numbers from the user in a list Nums.
Write separate user defined functions to perform the following operations:
▪ PushBig() - To push which have 5 or more digits into the stack, BigNums
▪ PopBig() - To pop and display the elements of the stack and display ‘Stack Empty’ if there
are no more elements.
Q3. Write a Python program to create a list of 10 integers. Write separate user defined functions to
perform the following operations:
▪ Create two stacks to store even and odd numbers.
▪ Pop and display the content of the even and odd stack.
For Example, If the list contains: N=[12,13,34,56,21,79,98,22,35,38]
Output: Even Stack : 38 22 98 56 34 12 Odd Stack: 35 79 21 13
Q4. Write a Python program to create a list of 10 integers. Write separate user defined functions to
perform the following operations:
● Push numbers divisible by either 3 or 5 into a stack.
● Pop and display the content of the stack.
For Example, List N = [12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 35 21 12
Q5. Write a Python program to input a sentence and store all the words in a list.[ Hint: Use Split() ] .
Write separate user defined functions to perform the following operations:
● Push all words starting with the alphabet ‘i’ or ‘I’ into a stack.
● Pop and display the content of the stack .
For Example, If the sample string is :
Incredible India campaign was a initiative by Ministry of Tourism
The Output of the code should be: initiative India Incredible
Q6. Write a Python program to create a list of n words.
Write separate user defined functions to perform the following operations:
● Push words that do not contain a vowel into a stack.
● Pop and display the content of the stack
Also, display the number of words not containing a vowel.
For Example,
words = ['Why','How','Myth','Fry','Cyst','Lynch','Ladder','Close','Rhythm' , 'Bond']
The Output should be:
Rhythm Lynch Cyst Fry Myth Why
Number of non vowel words in stack are 6
Q7. Write a Python program to create a list of 5 words.
Write separate user defined functions to perform the following operations:
● Push palindrome words in a stack.
● Pop and display the content of the stack. Display “Stack Empty” when there are no elements in
the stack.
For Example:
words = ['peep' , 'wow' , 'noon' , 'moon' , 'defer' ]
Sample Output of the code should be: noon wow peep Stack Empty
Q8. A stack, named ClrStack, contains records of some colors. Each record is represented as a tuple
containing four elements —
ColorName, RED, GREEN, BLUE. ColorName is a string, and RED, GREEN, BLUE are integers. For
example, a record in the stack may be ('Yellow', 237, 250, 68).
Write the following user-defined functions in Python to perform the specified operations on ClrStack:
(i) push_Clr(ClrStack, new_Clr):
This function takes the stack ClrStack and a new record new_Clr as arguments and pushes this new
record onto the stack.
(ii) pop_Clr(ClrStack):
This function pops the topmost record from the stack and returns it. If the stack is already empty, the
function should display the message "Underflow".
(iii) isEmpty(ClrStack):
This function checks whether the stack is empty. If the stack is empty, the function should return True,
otherwise the function should return False.
Q9. Write a Python program to create a list, NList containing following record as list elements:
[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list.
Write the following user defined functions in Python to perform the specified operations on the
stack named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes a list object containing
name of the city and country, which are not in India and distance is less than 3500 km from Delhi.
(ii) Pop_element(): It pops the objects from the stack and displays them. Also, the function should
display “Stack Empty” when there are no elements in the stack.
For example: If the nested list contains the following data:
NList=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194],
["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]
The stack should contain:
['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Columbo', 'Sri Lanka']
The output should be:
['Columbo', 'Sri Lanka']
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar']
Stack Empty
Q10. Write the following user-defined functions in Python to implement a Stack:
(i) push_trail(N, myStack): Here N and myStack are lists, and myStack represents a stack. The
function should push the last 5 elements from the list N onto the stack myStack. For example, if the
list N is 1,2,3,4,5,6,71, 2, 3, 4, 5, 6, 71,2,3,4,5,6,7, then the function push_trail() should push the
elements 3, 4, 5, 6, 7 onto the stack. Therefore the value of the stack will be 3,4,5,6,73, 4, 5, 6,
73,4,5,6,7. Assume that N contains at least 5 elements.
(ii) pop_one(myStack): The function should pop an element from the stack myStack, and return this
element. If the stack is empty, then the function should display the message 'Stack Underflow', and
return None.
(iii) display_all(myStack): The function should display all the elements of the stack myStack, without
deleting them. If the stack is empty, the function should display the message 'Empty Stack'.