0% found this document useful (0 votes)
59 views

Question Bank Programs Python CS

The document contains a series of Python programming exercises focused on functions, file handling, and data structures. Each question requires writing a specific function to perform tasks such as counting occurrences, filtering data, and manipulating lists and dictionaries. Additionally, it includes tasks related to reading and processing text and binary files, demonstrating various programming concepts and techniques.

Uploaded by

pankaj chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Question Bank Programs Python CS

The document contains a series of Python programming exercises focused on functions, file handling, and data structures. Each question requires writing a specific function to perform tasks such as counting occurrences, filtering data, and manipulating lists and dictionaries. Additionally, it includes tasks related to reading and processing text and binary files, demonstrating various programming concepts and techniques.

Uploaded by

pankaj chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1

AEC (Anjali Engineering Classes)


PYQs – Python Programs
(Functions, Files & Stack)
Functions
Q1) Write a function dispBook(BOOKS) in Python, that takes a dictionary BOOKS as
an argument and displays the names in uppercase of those books whose name
starts with a consonant.
For example, Consider the following dictionary
BOOKS = {1:"Python", 2:"Internet Fundamentals ", 3:"Networking ", 4:"Oracle sets",
5:"Understanding HTML"}
The output should be:
PYTHON
NETWORKING
Q2) Write a Python Program containing a function FindWord(STRING, SEARCH),
that accepts two arguments : STRING and SEARCH, and prints the count of
occurrence of SEARCH in STRING. Write appropriate statements to call the
function.
For example, if STRING = "Learning history helps to know about history with interest
in history" and SEARCH = 'history', the function should display
The word history occurs 3 times.
Q3) Write a function Show_sal(EMP) in python that takes the dictionary, EMP as an
argument. Display the salary if it is less than 25000 Consider the following dictionary
EMP={1:18000,2:25000,3:28000:4:15000}
The output should be:
18000
15000
Q4) Write a function, VowelWords(Str), that takes a string as an argument and
returns a tuple containing each word which starts with an vowel from the given string
For example, if the string is "An apple a day keeps the doctor away”,
the tuple will have (“An”,”apple”,”a”, “away”)
Q5) Write a function EVEN_LIST(L), where L is the list of elements passed as
argument to the function. The function returns another list named ‘even list’ that
stores even numbers in the list.
For example: If L contains [1,2,3,4,5,6,7,8]

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
2

The even list will have - [2,4,6,8]


Q6) Write a function shiftn(L,n), where L is a list of integers and n is an integer. The
function should return a list after shifting n number of elements to the left.
Example: If the list initially contains [2, 15, 3, 14, 7, 9, 19, 6, 1, 10] and n=2 then
function should return [3, 14, 7, 9, 19, 6, 1, 10, 2, 15]
If the list initially contains [2, 15, 3, 14, 7, 9, 19, 6, 1, 10] and n=4 then function
should return [7, 9, 19, 6, 1, 10, 2, 15, 3, 14]
Q7) Write a function INDEX_LIST(L), where L is the list of elements passed as
argument to the function. The function returns another list named ‘indexList’ that
stores the indices of all Non- Zero Elements of L.
For example: If L contains: [2, 0, 5, 0, 1, 0, 0]
The indexList will have: [0,2,4]
Q8) Write definition of a function Count_How_Many(Data, item) to count and display
number of times the value of item is present in the list Data. (Note: don’t use the
count() function)
For example : If the Data contains [101,102,107,105,102,103,104,102] and item
contains 102
The function should display 102 found 3 Times.
Q9) Write a function countNow(PLACES) in Python, that takes the dictionary,
PLACES as an argument and displays the names (in uppercase)of the places whose
names are longer than 5 characters.
For example, Consider the following dictionary
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}
The output should be:
LONDON
NEW YORK
Q10) Write a function, lenWords(STRING), that takes a string as an argument and
returns a tuple containing length of each word of a string.
For example, if the string is "Come let us have some fun",
the tuple will have (4, 3, 2, 4, 4, 3)
Q11) Write a function INDEX_LIST(L), where L is the list of elements passed as
argument to the function. The function returns another list named ‘indexList’ that
stores the indices of numbers divisible by 10 Elements of L.
For example: If L contains [20,40,12,10,23,50]
The indexList will have - [0,1,3,5]

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
3

Q12) Write a function merge_tuple(t1,t2), where t1 and t2 are the tuples of elements
passed as argument to the function. The function returns another tuple named that
stores the sum of consecutive element of t1 and t2, only if they are of same length
else return -1
For example: T1=(1,2,3,4) T2=(5,6,7,8)
Then function should return (6,8,10,12)
And if T1=(1,2,3) T2=(5,6,7,8)
Then function should return -1
Q13) Write a function SumDiv(L,x), where L is a list of integers and x is an integer;
passed as arguments to the function. The function returns the sum of elements of L
which are divisible by x or x+1.
For example, If L Contains [10, 27, 12, 20, 22] and x is 5
Then function returns 42 (10+12+20)
Q14) Write a function EvenOdd(L) in python, to add 1 in all odd values and 2 in all
even values of the list L.
Example: If original elements of the list L are: L= [35, 12, 16, 69, 26]
The modified list will be: L= [36, 14, 18, 70, 28]
Q15) Write a function even(L), where L is the list of element passed as argument to
the function. The function return another list named changelist that stores even
numbers.
Sample List:[1,2,3,4,5,6,7,8,9]
Expected Result:[2,4,6,8]

Text Files
Q1) Write a function COUNT() in Python to read from a text file 'Gratitude.txt' and
display the count of the letter 'e' in each line Example: If the file content is as follows:
Gratitude is a humble heart's radiant glow, A timeless gift that nurtures and bestows.
It's the appreciation for the love we're shown, In moments big and small, it's truly
known.
The COUNT() function should display the output as:
Line 1 : 3
Line 2 : 4
Line 3 : 6
Line 4 : 1

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
4

Q2) Write a function Start_with_I() in Python, which should read a text file
'Gratitude.txt' and then display lines starting with 'I'.
Example: If the file content is as follows:
Gratitude is a humble heart's radiant glow,
A timeless gift that nurtures and bestows.
It's the appreciation for the love we're shown,
In moments big and small, it's truly known.
Then the output should be
It's the appreciation for the love we're shown,
In moments big and small, it's truly known.
Q3) Write a function in Python to read a text file, Rhyme.txt and displays those words
which have length more than 5
Q4) Write a function, in Python that counts the number of lines in text file named
“data.txt” and displays the lines which are starting with “K” or ‘k’.
Q5) Write a function COUNTOWEL() IN PYTHON which should read each character
of a text file CHARACTER.TXT and display the count of vowels Example: If the file
content is as follows:
Exam is going on well
The COUNTOWEL() function should display the output as: 7
Q6) Write a method COUNTLINES() in python to read lines from text file
MYSTORY.TXT and display the count of lines which are starting with letter T
Example:if the file content is as follows:
Trees are the precious We should protect trees This way we can serve nature
The COUNTLINES() function should display output as:
The number of lines starting with letter T :2
Q7) A text file “PYTHON.TXT” contains alphanumeric text. Write a program that
reads this 3 text file and writes to another file “PYTHON1.TXT” entire file except the
numbers or digits in the file.
Q8) Write a method beginA() in Python to read lines from a text file Notebook.TXT,
and display those lines, which are starting with ‘A’.
For example If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
5

The beginA() function should display the output as:


An apple a day keeps the doctor away.
A marked difference will come in our country.
Q9) Write a method count_lines( ) in Python to read lines from text file ‘student.txt’
and display the total number of line in file and lines which are ending with ‘y’ alphabet
and not ending with ‘y’ separately.
Example: If the file content is as follows:
An apple in a day keeps the doctor away.
We should aware for everyone’s safety and security.
India is one of the biggest country in word.
The count_lines( ) function should display the output as:
The number of lines in file are: 3
The number of lines ending with alphabet ‘y’ are: 2
The number of lines not ending with alphabet ‘y’ are: 1
Q10) A pre-existing text file data.txt has some words written in it. Write a python
function displaywords( ) that will print all the words that are having length greater
than 3. If the contents of file is :
A man always wants to strive higher in his life He wants to be perfect.
The output should be:
always wants strive higher life wants perfect.
Q11) Write a function in Python to read a text file, Alpha.txt and displays those lines
which begin with the word ‘You’.
Q12) Write a function, vowelCount() in Python that counts and displays the number
of vowels in the text file named Poem.txt.
Q13) Write a method COUNTLINES() in Python to read lines from text file
‘TESTFILE.TXT’ and display the lines which are not starting with any vowel.
Example: If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES() function should display the output as:
The number of lines not starting with any vowel - 1
Q14) Write a function countwords( ) that read a file ‘python.txt’ and display the total
number of words which begins by uppercase character.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
6

Example: Suppose the file have following text:


‘Python is a powerful, user friendly and platform independent Language’
Output of function should be : 2
Q15) Write a function AMCount() in Python, which should read each character of a
text file STORY.TXT, should count and display the occurance of alphabets A and M
(including small cases a and m too).
Example: If the file content is as follows:
Updated information As simplified by official websites.
The EUCount() function should display the output as:
A or a:4
M or m :2
Q16) Assume that a text file named TEXT1.TXT already contains some text written
into it, write a program with a function named vowelwords(), that reads the file
TEXT1.TXT and create a new file named TEXT2.TXT , which shall contain only
those words from the file TEXT1.TXT which don’t start with an uppercase vowel(i.e.
with ‘A’,’E’,’I’,’O’,’U’) for example if the file TEXT1.TXT contains
Carry Umbrella and Overcoat When it Rains
then the file TEXT2.TXT shall contain
Carry and when it Rains.
Q17) Write a Python program to count all the line having 'a' as last character.
Q18) Write a function countbb() in Python, which reads the contents from a text file
‘article.txt’ and displays number of occurrences of words ‘bat’ and ‘ball’ (in all
possible cases) to the screen.
Example: If the content of file article.txt is as follows:
Bat and ball games are field games played by two opposing teams. Action starts
when the defending team throws a ball at a dedicated player of the attacking team,
who tries to hit it with a bat and run between various safe areas in the field to score
runs (points). The defending team can use the ball in various ways against the
attacking team's players to force them off the field when they are not in safe zones,
and thus prevent them from further scoring. The best known modern bat and ball
games are cricket and baseball, with common roots in the 18th-century games
played in England.
The countbb() function should display as:
Number of times bat occurs is 3
Number of times ball occurs is 5

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
7

Q19) Write a function CountYouMe( ) in python which reads the content of a text file
“BIOGRAPHY.TXT” and counts the words ‘You’ and ‘Me’ separately. (Not case
sensitive). Example: If the contents in “BIOGRAPHY.TXT” are as follows:
You are a hero for me you gifted a book to me which helped me a lot to learn the
programming It gave me a chance to develop business software
The output of the function should be:
Count of You in file: 2
Count of Me in file: 4
Q20) Write a function CountDigits( ) in python which reads the content of a text file
“MyData.TXT” and counts number of digits present in the text file.
Example: If the contents in “MyData.TXT” are as follows:
In 2023 we 22 students will appear in exam for 5 subjects
The output of the function should be:
No. of digits are: 7

Binary Files

Q1) Consider a binary file 'INVENTORY.DAT' that stores information about products
using tuple with the structure (ProductID, ProductName, Quantity, Price).
Write a Python function expensiveProducts() to read the contents of
'INVENTORY.DAT' and display details of products with a price higher than Rs. 1000.
Additionally, calculate and display the total count of such expensive products.
For example: If the file stores the following data in binary format
(1, 'ABC', 100, 5000)
(2, 'DEF', 250, 1000)
(3, 'GHI', 300, 2000)
then the function should display
Product ID: 1
Product ID: 3
Total expensive products: 2
Q2) Consider a file FLIGHT.DAT containing multiple records. The structure of each
record is as shown below:
[Fno, FName, Fare, Source, Destination]

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
8

Write a function COPY_REC() in Python that copies all those records from
FLIGHT.DAT where the source is DELHI and the destination is MUMBAI, into a new
file RECORD.DAT
Q3) Consider a Binary file BOOK.DAT containing a dictionary having multiple
elements. Each element is in the form
BNO:[BNAME,BTYPE,PRICE] as key:value pair where
BNO – Book Number BNAME – Book Name BTYPE - Book Type PRICE – Book
price
Write a user-defined function, findBook(price), that accepts price as parameter and
displays all those records from the binary file BOOK.DAT which has a book price
more than or equal to the price value passed as a parameter.
Q4) Consider a binary file “book.dat” that has structure [BookNo, Book_Name,
Author, Price].
Write a user defined function CreateFile() that takes input data for a record and add
to book.dat
Q5) Consider a binary file “MyFile.dat” that has following structure [ empid, ename
and salary]. Write a userdefined function to search records based on the salary
entered by the user and if the salary is more than 25000 then display the record.
Q6) Mayur is a student, who have a binary file called STUDENT.DAT containing
employee information- sid, name and age of each student. [sid, name , age]
Write the user defined function Get_Stud() to display the name and age of those
student who have a age greater than 18 year. In case there is no student having age
>18 the function displays message “There is no student who is greater than 18 year”.
Q7) Abhay have a binary file called library.dat containing book information- B_id,
B_name and B_price of each book.
[[B_id, B_name, B_price],[B_id, B_name, B_price],…]
Write the user defined function Trace_Book() to show the records of books having
the price less than 1000.
Q8) Consider a file, SPORT.DAT, containing records of the following structure:
[SportName, TeamName, No_Players]
Write a function, copyData(), that reads contents from the file SPORT.DAT and
copies the records with Sport name as “Basket Ball” to the file named BASKET.DAT.
The function should return the total number of records copied to the file
BASKET.DAT.
Q9) A Binary file, CINEMA.DAT has the following structure: {MNO:[MNAME,
MTYPE]} Where MNO – Movie Number MNAME – Movie Name MTYPE is Movie
Type Write a user defined function, findType(mtype), that accepts mtype as

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
9

parameter and displays all the records from the binary file CINEMA.DAT, that have
the value of Movie Type as mtype.
Q10) A binary file “emp.dat” has structure [EID, Ename, designation, salary]. I.Write
a user defined function CreateEmp() to input data for a record and add to emp.dat.
II.Write a function display() in Python to display the detail of all employees.
Q11) A binary file “PRODUCT.dat” has the following structure: {“pno”:PNo,
“pname”:Pname, “brand”:Brand, “price”:Price} i. Write a user defined function
WriteProduct( ) to input data for 7 products and add them to PRODUCT.dat . ii. Write
a function CountRecord(Brand) in Python which accepts the brand name as
parameter and count and return number of products of the given brand which are
stored in the binary file “PRODUCT.dat” .
Q12) Consider a Binary file Employee.dat containing details such as
empno:ename:salary(separator ‘:’). Write a Python function to display details of
those employees who are earning between 20000 and 40000(both values inclusive)

CSV Files
Q1) Mandeep is a Python programmer working in C-company. For storing details of
employees working in the company, he has created a csv file named record.csv, to
store the The structure of record.csv is : [Emp_Id, Emp_Name, Mobile, Salary]
Where Emp_Id is Employee ID (integer) Emp_Name is Employee Name (string)
Mobile is to store mobile number of employee (integer) Salary – Salary earned by
the employees(integer)
Mandeep want to write program in Python that defines and calls the following user
defined functions:
a) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each
record consists of a list with field elements as empid, name and mobile andemployee
salary respectively.
b) COUNTR() – To count the number of records present in the CSV file named
‘record.csv’. As python expert help him complete the task
Q2) Write a Program in Python that defines and calls the following user defined
functions:
(i) add() – To accept and add data of a product to a CSV file ‘product.csv’.
Each record consists of a list with elements as pid, pnameand priceto
storeproduct id, product name and pricerespectively.
(ii) search()- To display the records of the products whose price is more
than5000.
Q3) Write a Program in Python that defines and calls the following user defined
functions:

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
10

(i) INSERT() – To accept and add data of a student to a CSV file ‘student.csv’.
Each record consists of a list with field elements as sid, name and marksto
store student id, name and marks respectively.
(ii) COUNTSTUDENTS() – To count the number of records present in theCSV
file named‘student.csv’.
Q4) Write a Program in Python that defines and calls the following user defined
functions:
(i) add() – To accept and add data of an employee to a CSV file
‘empdata.csv’. Each record consists of a list with field elements as eid,
ename and salary to store emp id, emp name and emp salary respectively.
(ii) search()- To display the records of the emp whose salary is more than
10000.
Q5) Write a python program to create a csv file dvd.csv and write 10 records in it
Dvdid, dvd_name, qty, price. Display those dvd details whose dvd price is more than
25.
Q6) Vedansh is a Python programmer working in a school. For the Annual Sports
Event, he has created a csv file named Result.csv, to store the results of students in
different sports events.
The structure of Result.csv is : [St_Id, St_Name, Game_Name, Result]
Where St_Id is Student ID (integer) ST_name is Student Name (string) Game_Name
is name of game in which student is participating(string) Result is result of the game
whose value can be either 'Won', 'Lost' or 'Tie' For efficiently maintaining data of the
event, Vedansh wants to write the following user defined functions:
Accept() – to accept a record from the user and add it to the file Result.csv. The
column headings should also be added on top of the csv file.
wonCount() – to count the number of students who have won any event. As a
Python expert, help him complete the task.
Q7) Write a Program in Python that defines and calls the following user defined
functions:
(i) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’.
Each record consists of a list with field elements as empid, name and
mobile to store employee id, employee name and employee salary
respectively.
(ii) COUNTR() – To count the number of records present in the CSV file
named ‘record.csv’.
Q8) Write a Program in Python that defines and calls the following user defined
functions:
(i) add() – To accept and add data of an employee to a CSV file ‘furdata.csv’.
Each record consists of a list with field elements as fid, fname and fprice to
store furniture id, furniture name and furniture price respectively.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
11

(ii) search()- To display the records of the furniture whose price is more than
10000.
Q9) Write a program to
(a) add/insert records in file “data.csv”. Structure of a record is roll number, name
and class.
(b) search and display record for a given class
Q10) What is the difference between CSV file and Text File? Write a program in
Python that defines and calls the following functions:
Insert() – To accept details of clock from the user and stores it in a csv file
‘watch.csv’. Each record of clock contains following fields – ClockID, ClockName,
YearofManf, Price. Function takes details of all clocks and stores them in file in one
go.
Delete() – To accept a ClockID and removes the record with given ClockID from the
file ‘watch.csv’. If ClockID not found then it should show a relevant message. Before
removing the record it should print the record getting removed.
Q11) Give one advantage of using CSV file over Binary file. Write a program in
Python that defines and calls the following functions:
saving() – To accepts details of equipments from the user in following format (ID,
Name, Make, Price) and save it in a csv file ‘parts.csv’. Function saves one record at
a time.
search() – To accept two prices and displays details of those equipments which has
price between these two values.

Stack
Q1) Given a Dictionary Stu_dict containing marks of students for three test-series in
the form Stu_ID:(TS1, TS2, TS3) as key-value pairs. Write a Python program with
the following user-defined functions to perform the specified operations on a stack
named Stu_Stk
(i) Push_elements(Stu_Stk, Stu_dict) : It allows pushing IDs of those
students, from the dictionary Stu_dict into the stack Stu_Stk, who have
scored more than or equal to 80 marks in the TS3 Test.
(ii) Pop_elements(Stu_Stk): It removes all elements present inside the stack
in LIFO order and prints them. Also, the function displays 'Stack Empty'
when there are no elements in the stack.
Call both functions to execute queries. For example: If the dictionary Stu_dict
contains the following data:
Stu_dict ={5:(87,68,89), 10:(57,54,61), 12:(71,67,90), 14:(66,81,80),
18:(80,48,91)}
After executing Push_elements(), Stk_ID should contain [5,12,14,18]

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
12

After executing Pop_elements(), The output should be:


18
14
12
5
Stack Empty
Q2) Write a function in Python, Push(Cosmetics) where, Cosmetic is a dictionary
containing the details of products– {Pname:price}. The function should push the
names of those products in the stack whose price is greater than 130. Also display
the count of elements pushed into the stack.
For example: If the dictionary contains the following data:
Ditem = {“FaceWash”:105, "Facepack":150, "CleansingMilk":130, "Sunscreen": 180,
"FaceMask":115}
The stack should contain
Facepack Sunscreen
The output should be:
The count of elements in the stack is 2
Q3) A list named as Record contains following format of for students: [student_name,
class, city]. Write the following user defined functions to perform given operations on
the stack named ‘Record’:
(i) Push_record(Record) – To pass the list Record = [ ['Rahul', 12,'Delhi'],
[‘Kohli',11,'Mumbai'], ['Rohit',12,'Delhi'] ] and then Push an object
containing Student name, Class and City of student belongs to ‘Delhi’ to
the stack Record and display and return the contents of stack
(ii) Pop_record(Record) – To pass following Record [[“Rohit”,”12”,”Delhi”]
[“Rahul”, 12,”Delhi”] ] and then to Pop all the objects from the stack and at
last display “Stack Empty” when there is no student record in the stack.
Thus the output should be: -
[“Rohit”,”12”,”Delhi”]
[“Rahul”, 12,”Delhi”]
Stack Empty
Q4) A list contains following record of a student: [student_name, age, hostel] Write
the following user defined functions to perform given operations on the stack named
‘stud_details’:
(i) Push_element() - To Push an object containing name and age of students
who live in hostel “Ganga” to the stack

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
13

(ii) Pop_element() - To Pop the objects from the stack and display them. Also,
display “Stack Empty” when there are no elements in the stack.
For example: If the lists of customer details are:
[“Barsat”,17,”Ganga”] [“Ruben”, 16,”Kaveri”] [“Rupesh”,19,”Yamuna”]
The stack should contain [“Barsat”,17,”Ganga”]
The output should be:
[“Barsat”,17,”Ganga”]
Stack Empty
Q5) Write a function in Python, Push(EventDetails) where , EventDetails is a
dictionary containing the number of persons attending the events– {EventName :
NumberOfPersons}. The function should push the names of those events in the
stack named ‘BigEvents’ which have number of persons greater than 200. Also
display the count of elements pushed o n to the stack.
For example:
If the dictionary contains the following data:
EventDetails ={"Marriage":300, "Graduation Party":1500, "Birthday Party":80, "Get
together" :150}
The stack should contain : Marriage Graduation Party
The output should be:
The count of elements in the stack is 2
Q6) A nested list contains the data of visitors in a museum. Each of the inner lists
contains the following data of a visitor: [V_no (int), Date (string), Name (string),
Gender (String M/F), Age (int)] Write the following user defined functions to perform
given operations on the stack named "status": (i) Push_element(Visitors) - To Push
an object containing Gender of visitor who are in the age range of 15 to 20. (ii)
Pop_element() - To Pop the objects from the stack and count and display the number
of Male and Female entries in the stack. Also, display “Done” when there are no
elements in the stack. For example: If the list of Visitors contains
[['305', "10/11/2022", “Geeta”,"F”, 35],
['306', "10/11/2022", “Arham”,"M”, 15],
['307', "11/11/2022", “David”,"M”, 18],
['308', "11/11/2022", “Madhuri”,"F”, 17],
['309', "11/11/2022", “Sikandar”,"M”, 13]]
The stack should contain
F

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
14

M
M
The output should be:
Female: 1
Male: 2
Done
Q7) A list of numbers is used to populate the contents of a stack using a function
push(stack, data) where stack is an empty list and data is the list of numbers. The
function should push all the numbers that are even to the stack. Also write the
function pop(stack) that removes and returns the top element of the stack on its each
call. Also write the function calls.
Q8) A list, NList contains 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
Q9) A list contains following record of a customer: [Customer_name, Phone_number,
City] Write the following user defined functions to perform given operations on the
stack named ‘status’:
(i) Push_element() - To Push an object containing name and Phone number of
customers who live in Goa to the stack

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
15

(ii) Pop_element() - To Pop the objects from the stack and display them. Also,
display “Stack Empty” when there are no elements in the stack.
For example: If the lists of customer details are:
[“Gurdas”, “99999999999”,”Goa”] [“Julee”, “8888888888”,”Mumbai”]
[“Murugan”,”77777777777”,”Cochin”] [“Ashmit”, “1010101010”,”Goa”]
The stack should contain [“Ashmit”,”1010101010”] [“Gurdas”,”9999999999”]
The output should be:
[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
Stack Empty
Q10) Write a function in Python,
Push(SItem) where , SItem is a dictionary containing the details of stationary items–
{Sname:price}. The function should push the names of those items in the stack who
have price greater than 75. Also display the count of elements pushed into the stack.
For example: If the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain Notebook Pen
The output should be:
The count of elements in the stack is 2
Q11) Rajiv has created a dictionary containing employee names and their salaries as
key value pairs of 6 employees. Write a program, with separate user defined
functions to perform the following operations: ● Push the keys (employee name) of
the dictionary into a stack, where the corresponding value (salary) is less than
85000. ● Pop and display the content of the stack. For example: If the sample
content of the dictionary is as follows: Emp={"Ajay":76000, "Jyothi":150000,
"David":89000, "Remya":65000, "Karthika":90000, "Vijay":82000} The output from the
program should be: Vijay Remya Ajay
Q12) Aruna has a list containing temperatures of 10 cities. You need to help her
create a program with separate user defined functions to perform the following
operations based on this list. 3 ● Traverse the content of the list and push the
negative temperatures into a stack. ● Pop and display the content of the stack. For
Example: If the sample Content of the list is as follows: T=[-9, 3, 31, -6, 12, 19, -2,
15, -5, 38] Sample Output of the code should be: -5 -2 -6 -9
Q13 A nested list contains the records of Mobiles in the following format: [[modelno,
name, price], [modelno, name, price], [modelno, name, price],….] Write the following
user defined functions to perform given operations on the stack named Mobile: a.
Push operation – To push details (modelno, name, price) of those mobiles which has
price lower than 10000. Make a note that there cannot be more than 20 elements in

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
16

stack Mobile. b. Pop operation – To pop elements from stack one by one and display
them. Also, display “Underflow” when stack becomes empty. For example, If the list
contains [[‘V20’, ‘Vivo 20 SE’, 18000], [‘S11’, ‘Lava S11’, 8900], [‘i88’, ‘IPro 88 SE’,
6500], [‘ip13’, ‘iPhone 13’, 125000]] The stack should contain: [‘i88’, ‘IPro 88 SE’,
6500] [‘S11’, ‘Lava S11’, 8900] The Output should be: [‘i88’, ‘IPro 88 SE’, 6500]
[‘S11’, ‘Lava S11’, 8900] Underflow
Q14) A Dictionary Medal contains the details of schools and medals won by them in
following format {school_name:medals_won}. Write a function Push(Medal) in
Python that pushes those school names in stack named SCHOOL which has won
more than 3 medals. Maximum capacity of stack SCHOOL is 15. Function also
shows number of items pushed in stack. If number of items exceeds 15 then it shows
OVERFLOW. For example: If dictionary Medal contains {‘KV1’:5, ‘KV2’:2, ‘KV3’:4,
‘KV4’:1, ‘KV5’:7} Then stack should contain KV5 KV3 KV1 The output should be:
Number of item pushed in stack Medal are 3
Q15) Arun has created a dictionary containing employee name and their salary as
key value pair of 05 employees. Create user defined functions to perform the
following operations: i. 3 Push the keys (name of employee) of dictionary into the
stack where the corresponding value (salary) is more than 20000. ii. Delete the
element from the stack and display the stack after deletion of the element.
Q16) Write a function in Python, Push(Client) where , Client is a dictionary containing
the details of clients– {Cname:age}. The function should push the names of those
client in the stack who have age greater than 50. Also display the count of elements
pushed into the stack. For example: If the dictionary contains the following data:
Cname={"Robert":55,"Jhon":35,"Smith":75,"Reyan":25} The stack should contain
Robert Smith The output should be: The count of elements in the stack is 2
Q17) Write Addnew(Book) and Remove( Book) functions in Python to Add a new
Book and Remove a Book from a List of Books , considering them to act as PUSH
and POP operations of the data structure Stack .

MYSQL Connectivity
Q1) Sarithahas created a table Inventory in MYSQL database, warehouse:
Inv_No(Inventory Number )- integer Inv_name(Name) – string Inv_Entry(Date )
Inv_price – Decimal Note the following to establish connectivity between Python and
MySQL: Username - root Password - 12345 Host - localhost Saritha, now wants to
delete the records of inventory whose price is more than 1000. Help Saritha to write
the program in Python.
Q2) Kavyawants to write a program in Python to insert the following recordin the
table named Inventory in MYSQL database, WAREHOUSE: Inv_No(Inventory
Number )- integer Inv_name(Name) – string Inv_Entry(Date ) Inv_price – Decimal
Note the following to establish connectivity between Python andMySQL: Username -
root Password - 12345 Host - localhost The values of fields Inv_No, Inv_name,

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)
17

Inv_Entryand Inv_price has to be accepted fromthe user. Help Kavyato write the
program in Python.
Q3) (ii) Virat has created a table named TRAVELS in MySQL: Tour_ID – string
Destination – String Geo_Cond– String Distance – integer (In KM) Note the following
to establish connectivity between Python and MYSQL: • Username is root •
Password is bharat • The table TRAVELS exists in a MYSQL database named
TOUR. • The details Tour_ID, Destination, Geo_Cond and Distance are to be
accepted from the user. Virat wants to display All Records of TRAVELS relation
whose Geographical condition 1+4=5 8 | Page is hilly area and distance less than
1000 KM. Help Virat to write program in python.
Q4) Aarya has created a table named Emp in MySQL: EmpNo – integer EmpName
– string Age– integer Salary – integer Note the following to establish connectivity
between Python and MYSQL: • Username - root • Password - tiger • Host - localhost
• The Emp table exists in a MYSQL database named company. • The details of Emp
table (EmpNo, EmpName, Age and Salary) Aarya wants to display All Records of
Emp relation whose age is greater than 55. Help Aarya to write program in python.
Q5) Kabir wants to write a program in Python to insert the following record in the
table named Student in MYSQL database, SCHOOL: • rno(Roll number )- integer •
name(Name) - string • DOB (Date of birth) – Date • Fee – float Note the following to
establish connectivity between Python and MySQL: • Username - root • Password -
tiger • Host - localhost The values of fields rno, name, DOB and fee has to be
accepted from the user. Help Kabir to write the program in Python.
Q6) Sartaj has created a table named Student in MYSQL database, SCHOOL: •
rno(Roll number )- integer • name(Name) - string • DOB (Date of birth) – Date • Fee
– float
Note the following to establish connectivity between Python and MySQL: •
Username - root • Password - tiger • Host – localhost
Sartaj, now wants to display the records of students whose fee is more than 5000.
Help Sartaj to write the program in Python.

Youtube : Anjali Luthra, Instagram : aec_computer_education, Linkedin : Anjali Luthra, Google Play Store: ALCoders
Courses Available for Computer Science related Subjects for School(CBSE, ISC, IB) & College (BCA, B.Tech, B.Sc. CS & IT)

You might also like