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

class 12 cs project hdb in python

Class 12 python programming proget
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

class 12 cs project hdb in python

Class 12 python programming proget
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

SESSION : 2024-25

PRACTICAL FILE 2024-25


SUBMITTED BY: SUBMITTED TO:

NAME: Ujjwal Chauhan HITESH PUJARI

CLASS: XII ‘G’

ROLL NO.:-24
S.NO TOPIC REMARK

1 Write a program using user defined function


FIND_LEN(str) that reads a string/line and print each
individual word of the line along with the length of the
word.

2
Write a program using user defined function
SQUARE_LIST(L), where L is the list of elements passed as
argument to the function. The function returns another list
named ‘SList’ that stores the Squares of all Non-Zero
Elements of L.

3
Write a function program using user defined function
REVERSE_DATA(str) that reads a string/sentence and
create a new sentence/ string storing each of the word in its
reverse form.

4 Write a program that allow the user to enter the eid, ename,
age, dept of n employees and write the detail of these
employees in the text file “emp.text”.

5
Write a program to add two more records i.e. detail of two
more employees in the file “emp.text” created in the program
no. 3 and then display the contents of the file.

6
Write a program using user defined function count_line() that count
the number of lines starting with letter ‘A’ or ‘T’ from the file
“data.text” and display it on the screen.

7
Write a program using user defined function count_word() to count
the words "are" and "the" in the file “stud.text”.

8
Write a program using user defined function count_vowel() to count
the number of vowels in the file “data.txt”

9 Write a program to write multiple records of students(rno,


name, subject, marks) in form of lists in a file "dps.dat" and
then display all data on the screen.

10 Write a program using user defined function CopyBinary()


that will copy the data from the binary file "source.dat" to
"target.dat"

11
Write a program to display the detail of the
employee from the file "emp.dat" as per the eid
entered.
12
Consider a binary file ”flight.dat” that store the
information . WAP to display information of those
flights whose starting point is Delhi.
13 Write a program using user defined function in
Python WordCount() to read a text file
“Mydiary.txt” and display the number of words
present in each line

14 Write a program using user defined function


INDEX_LIST(L), where L is the list of elements
passed as argument to the function. The function
returns another list named ‘IndList’ that stores the
indices of all Non-Zero Elements of L.

15 Consider the binary file “emp.dat” that contains


the information of employees like (eid, ename,
salary) in form of list. Write a program to increase
the salary of those employees by 2000 whose salary
is less than 25000 from the binary file “emp.dat”.

16
WAP to create a csv file “student.csv” to store data
Obtain the detail of five students from the user and
write it in the file
17 Sameer is a Python programmer. He has created a
csv file to store the details of the different
furnitures in form a list

18 Tushar is a Python programmer. For the Annual


Examination , he has created a csv file to store the
result of the students in different subjects in form a
list
19 Write SQL query for creating table according to question

20 Write the SQL commands for the question from (i) to (x) on
the basis of table TEACHER given below and Write SQL
commands for the following statements

21 Consider the following tables STORE and SUPPLIERS and


answer the given questions

22 Consider the above tables STORE and SUPPLIERS


given in Q.No. 18 and Give the output of the following
SQL queries

23 Consider the following tables STORE and SUPPLIERS and


answer the given questions and Write SQL commands for
the following statements

24 Consider the table Student given in Q.No. 23 and


give the output of the following SQL queries

25 Consider the following tables CLUB and COACH.


Write SQL queries for the following statements

26 Consider the following tables CLUB and


COACHES given in Give the output of the
following SQL queries

27 A list, NList contains following record as list


elements: [RollNo, SName, marks] 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

28 You need to help him create a program with


separate user defined functions to perform the
following operations based on this list.

29 You need to help him create a program with


separate user defined functions to perform the
following operations based on this list.

30
Consider the table “student” in database
“test”WAP to display the detail of all students from
the table “student” of database “dps”.

31
WAP to display the detail of student whose rollno is entered
by the user from the table “student” of database “dps”

32
WAP to display the detail of all students who have
submitted the project from the table “student” of
database “test”.
33
WAP to insert a new record in the table “student”
of database “dps”.
34
WAP to display to increase the marks by 5 of all the
students of section ‘A’ from the table “student” of
database “dps”.
35
WAP to display to remove the record of the students whose
rollno is entered by the user from the table “student” of
database “dps”.
Q1. Write a program using user defined function FIND_LEN(str) that reads a string/line and
print each individual word of the line along with the length of the word.

def FIND_LEN(string):

a=string.split()

for i in a:

print(i,", the length of string is",len(i))

string=input("enter a line")

FIND_LEN(string)

Q2. Write a program using user defined function SQUARE_LIST(L), where L is the list of
elements passed as argument to the function. The function returns another list named ‘SList’
that stores the Squares of all Non-Zero Elements of L.

def SQUARE_LIST(L):

b=0

a=[]

for i in L:

if i!=0:

b=i*i

a.append(b)

print(a)

c=[1,4,67,7,0,8,0]

SQUARE_LIST(c)
Q3. Write a function program using user defined function REVERSE_DATA(str) that reads
a string/sentence and create a new sentence/ string storing each of the word in its reverse
form.

def REVERSE_DATA(str1):

k = str1.split()

x = []

str2 = ""

for i in range(0,len(k)):

x.append(k[i][::-1])

for j in range(0,len(x)):

str2 = str2 + x[j] + " "

print("Reversed String :",str2)

str1 = input("Enter a string : ")

REVERSE_DATA(str1)

Q4. Write a program that allow the user to enter the eid, ename, age, dept of n employees and
write the detail of these employees in the text file “emp.text”.
def employee_details(n):

file = open("emp.text", "w")

for i in range(n):

print("Total employee :", i + 1)

eid = input("Enter ID: ")

ename = input("Enter Name: ")

age = input("Enter Age: ")

dept = input("Enter Department: ")

file.write("Employee ID: " + eid + ", Name: " + ename + ", Age: " + age + ", Department: " + dept )

file.close()

n = int(input("Enter the number of employees: "))

employee_details(n)

print("Employee details have been written to 'emp.text'.")

Q5. Write a program to add two more records i.e. detail of two more employees in the file
“emp.text” created in the program no. 3 and then display the contents of the file.
def add_2_det(a):

file = open("emp.text", "a")

for i in range(a):

print("Entering details for Employee", i + 1)

eid = input("Enter Employee ID: ")

ename = input("Enter Employee Name: ")

age = input("Enter Employee Age: ")

dept = input("Enter Employee Department: ")

file.write("Employee ID: " + eid + ", Name: " + ename + ", Age: " + age + ", Department: " + dept )

file.close()

add_2_det(2)

print("Employee details have been written to 'emp.text'.")


Q6. Write a program using user defined function count_line() that count the number of lines
starting with letter ‘A’ or ‘T’ from the file “data.text” and display it on the screen.
def count_line(file_name):

fp=open("data.txt")

for line in fp:

if line.strip().startswith(('a', 't')):

print(line)

file_name = "data.text"

result = count_line(file_name)
Q7. Write a program using user defined function count_word() to count the words "are" and
"the" in the file “stud.text”.

def count_word()

a=0

fp=open("stud.txt", 'r')

for i in fp:

words = i.strip().split()

a += words.count("are") + words.count("the")

print("Total numbers words 'are' and 'the'.",a)

result = count_word()
Q8. Write a program using user defined function count_vowel() to count the number of
vowels in the file “data.txt”
def count_vowel():

a=0

vowels = "aeiouAEIOU"

fp=open("data.txt", 'r')

for line in fp:

for char in line:

if char in vowels:

a += 1

print(a, " is the number of vowels in this text file.")

count_vowel()
Q9. Write a program to write multiple records of students(rno, name, subject, marks)
in form of lists in a file "dps.dat" and then display all data on the screen.

import pickle

def write_records():

fp= open("dps.dat", "wb")

n = int(input("Enter the number of students: "))

while True:

for i in range(n):

rno = int(input("Enter Roll Number: "))

name = input("Enter Name: ")

subject = input("Enter Subject: ")

marks = float(input("Enter Marks: "))

record = [rno, name, subject, marks]

pickle.dump(record, file)

ch=input(“Do you want to add more data?”)

if ch = “y”or “Y”:

break

print("Records written successfully to dps.dat.")


Q10. Write a program using user defined function CopyBinary() that will copy the data
from the binary file "source.dat" to "target.dat"
import pickle

def copybinary():

fp= open("source.dat", "rb")

fp2=open("target.dat","wb")

a=pickle.load(fp)

while True:

pickle.dump(a, fp2)

print("File Copied Successfully")

Q11. Write a program to display the detail of the employee from the file
"emp.dat" as per the eid entered by the user. Consider the binary file “emp.dat”
that contains the information of employees like (eid, ename, salary) in form of
list.
import pickle

def findemp():

fp= open("emp.dat", "rb")

eid=int(input("enter your eid"))

a=pickle.load(fp)

b=a.split(":")

for i in b:

if eid==int([0]):

print(i," is required employee")


Q12. Consider a binary file ”flight.dat” that store the information of flights
like (fno, source, destination) in form of list . WAP to display information
of those flights whose starting point is Delhi from the binary file
"flight.dat".

import pickle

def findflight():

fp=open("flight.dat","rb")

a=input("enter the name of place to find")

b=pickle.load(fp)

for i in len(b):

if i[2]==a:

print(" flight will start from ",a)


Q13. Write a program using user defined function in
Python WordCount() to read a text file “Mydiary.txt”
and display the number of words present in each line.

def WordCount():

fp=open("Mydiary.txt", "r")

no = 1

print("Word count for each line in Mydiary.txt")

for line in file:

words = len(line.split())

print(f"Line ",no,":", words ,"words")

no += 1

Q14. Write a program using user defined function


INDEX_LIST(L), where L is the list of elements passed as argument
to the function. The function returns another list named ‘IndList’
that stores the indices of all Non-Zero Elements of L.

def INDEX_LIST(l):

l2= []

for i in range(len(l)):

if l[i] != 0:

l2.append(i)

return l2

L = [12, 4, 0, 11, 0, 35]

result = INDEX_LIST(L)

print("List:", L)

print("Indices of non-zero elements:", result)


Q15. Consider the binary file “emp.dat” that contains the
information of employees like (eid, ename, salary) in form of list.
Write a program to increase the salary of those employees by
2000 whose salary is less than 25000 from the binary file
“emp.dat”.

import pickle

def update_salary():

with open("emp.dat", "rb") as file:

employees = []

while True:

record = pickle.load(file)

employees.append(record)

for employee in employees:

if employee[2] < 25000:

employee[2] += 2000

for employee in employees:

pickle.dump(employee, file)

print("Salaries are updated successfully")

update_salary()
Q16. WAP to create a csv file “student.csv” to store data (rollno,
name, marks). Obtain the detail of five students from the user and
write it in the file “student.csv”

import csv

def add_details():

fp= open("student.csv", "a")

for i in range(5):

rollno=int(input("enter rollno of student"))

name=input("enter name of student")

marks=int(input("enter marks of student"))

ab=[rollno,name,marks]

wo=csv.writer(fp)

wo.writerow(ab)

print("all data is add successfully")

add_details()
Q17. Sameer is a Python programmer. He has created a csv file named “furniture.csv” to store
the details of the different furnitures in form a list [Fid, Fname, Fprice] .Now Sameer wants to
write the following user defined functions:

(a) addDetail() – To accept the details of furniture items and add data in the csv file
“furniture.csv”. The column heading should also be added on top of the csv file.

(b) search()- To display the records of the furniture whose price is more than 10000

import csv

def addDetail():

fp= open("furniture.csv","w")

writer = csv.writer(fp)

n = int(input("Enter the number of furniture items you want to add: "))

for i in range(n):

fid = input("Enter Furniture ID: ")

fname = input("Enter Furniture Name: ")

fprice = float(input("Enter Furniture Price: "))

writer.writerow([fid, fname, fprice])

print("Details added successfully!")

def search():

fp2 open("furniture.csv", mode="r")

reader = csv.reader(file)

print("Furniture items with price greater than 10000:")

for row in reader:

if float(row[2]) > 10000:

print("Fid:" {row[0]}," Fname:" ,{row[1]}, "Fprice: ",{row[2]})


Q 18.Tushar is a Python programmer working in a school. For the Annual Examination , he
has created a csv file named dps.csv to store the result of the students in different subjects in
form a list

[Rollno, Name, Subject, Marks] . For efficiently maintaining data of the examination, Tushar
wants to write the following user defined functions:

(a) addStudent() – To accept the details of five students and store data [rollno, name,
subject, marks] in the csv file “dps.csv”.

search()- To display the records of the students whose marks is more than
import csv

def addStudent():

fp= open("dps.csv", mode="a")

writer = csv.writer(fp)

for i in range(5):

rollno = int(input("Enter Roll Number: "))

name = input("Enter Name: ")

subject = input("Enter Subject: ")

marks = int(input("Enter Marks: "))

writer.writerow([rollno, name, subject, marks])

print("Student details added successfully!")

def search():

fp2= open("dps.csv", mode="r")

reader = csv.reader(file)

print("Students with marks greater than 75:")

for row in reader:

if float(row[3]) > 75:

print("Rollno: ",{row[0]}," Name: ",{row[1]}," Subject:", {row[2]}," Marks: ",{row[3]})

Q.19 Write SQL query to create a table ‘Workshop’ with the following structure:
CREATE TABLE Workshop (

WorkshopId INTEGER PRIMARY KEY,

Title VARCHAR(30) NOT NULL,

WorkshopDate DATE,

NumSpeakers INTEGER,

Address VARCHAR(50)

);

Q.20 Write the SQL commands for the question from (i) to (x) on the basis of table
TEACHER given below:
i) DELETE FROM TEACHER WHERE TNo = 107;
ii) DELETE FROM TEACHER;
iii) ALTER TABLE TEACHER ADD PhoneNo VARCHAR(15);
iv) ALTER TABLE TEACHER MODIFY Tname VARCHAR(40)
v) SELECT Department, AVG(Salary) AS AvgSalary
FROM TEACHER
GROUP BY Department;

vi) UPDATE TEACHER

SET Salary = Salary + 2000

WHERE Department = 'English';

vii) UPDATE TEACHER

SET Salary = Salary + 3000;

viii) ALTER TABLE TEACHER DROP COLUMN Gender;

ix) SELECT *

FROM TEACHER

WHERE Tname LIKE 'R%';

x) SELECT TNo, Tname, Department

FROM TEACHER

WHERE Tname LIKE '%a';

Q21. Consider the following tables STORE and SUPPLIERS and answer the given
questions:
Table : STORE

Item No Item Scode Qty Rate LastBuy

2005 Sharpener Classic 23 60 8 31-Jun-09

2003 Ball Pen 0.25 22 50 25 01-Feb-10

2002 Gel Pen Premium 21 150 12 24-Feb-10


2006 Gel Pen Classic 21 250 20 11-Mar-09

2001 Eraser Small 22 220 6 19-Jan-09

2004 Eraser Big 22 110 8 02-Dec-09

2009 Ball Pen 0.5 21 180 18 03-Nov-09

SUPPLIER

i)SELECT *

FROM STORE

ORDER BY LastBuy ASC;

ii) SELECT ItemNo, Item FROM STORE

WHERE Rate > 15;

iii) SELECT *

FROM STORE

WHERE Scode = 22 OR Qty > 110;

iv) SELECT Scode, MIN(Rate) AS MinRate

FROM STORE

GROUP BY Scode;
Q 22.Consider the above tables STORE and SUPPLIERS given in Q.No. 18 and Give the
output of the following SQL queries:

1. SELECT COUNT(DISTINCT SCODE) FROM STORE;

2. SELECT RATE*QTY FROM STORE WHERE ITEMNO = 2004;

3. SELECT ITEM , SNAME FROM STORE S, SUPPLIER P WHERE S.SCODE


= P.SCODE AND ITEMNO = 2006;
SELECT MAX(LASTBUY) FROM STORE GROUP BY SCODE
1) 3
2) 880
3) Gel Pen Classic Premium Stationary
4) MAX(LASTBUY)
24-Feb-10
01-Feb-1
0 31-Jun-09
Q23. Consider the following table Student and answer the questions :

Table : Student

Roll Gende Depart


no Name Age r ment Fee
Comput
101 Ankit 20 M er 700
102 Payal 23 F Hindi 400
103 Mohit 27 M Maths 900
Priya Comput
104 nka 19 F er 700
105 Anjali 25 F English 600
Sanke
106 t 29 M Hindi 400
Comput
107 Rahul 31 M er 700
Rash
108 mi 22 F Maths 900
1) SELECT * FROM Student

WHERE Department = 'Computer';

2) SELECT Name FROM Student

WHERE Gender = 'M' AND Department = 'Maths';

3) UPDATE Student SET Fee = Fee + 100

WHERE Department = 'Computer';

4) SELECT AVG(Fee) AS AverageFee

FROM Student;

5) DELETE FROM Student

WHERE Rollno = 107;

Q24. Consider the table Student given in Q.No. 23 and give the output
of the following SQL queries:

1)4

2)900

3)Priyanka 19 Computer

Rahul 31 Computer

Rashmi 22 Maths

4) Name Age Gender

Ankit 20 M

Priyanka 19 F

Rahul 31 M

Payal 23 F

Sanket 29 M
Q25. Consider the following tables CLUB and COACH. Write SQL queries for
the following statements.

1) SELECT * FROM CLUB

WHERE Sports = 'SWIMMING';

2) SELECT CoachName, DateofApp FROM CLUB

ORDER BY DateofApp DESC;

3) SELECT CoachName, Age, Gender, (Pay * 0.15) AS Bonus

FROM CLUB;

4) SELECT CLUB.CoachName, COACHES.SportsPerson, CLUB.CoachID

FROM CLUB

INNER JOIN COACHES

ON CLUB.CoachID = COACHES.CoachID;

Q26. Consider the following tables CLUB and COACHES given in give the
output of the following SQL queries :
i)5

ii)34

iii) CoachID CoachName

101 KUKREJA

108 MOHIT

111 ABHISHEK

104 TARUN

iv) CoachName Age Sports SportsPerson Pay

KUKREJA 35 KARATE AJAY 1000

RAVINA 34 KARATE SEEMA 1200

TARUN 33 BASKETBALL VINOD 1500


Q27. A list, NList contains following record as list elements: [RollNo, SName,
marks] 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 marks which are more than 90 into the stack.
(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.

travel = []

def Push_Element(NList):

global travel

for i in NList[::-1]:

if i[2] > 90:

travel.append(i)

print(travel)

def Pop_Element():

global travel
print(travel.pop())

if len(travel)==0:

print("Stack is empty")

L = [[11, "Sarthak", 92], [12, "Hardik", 95], [13, "Ravi", 80], [14, "Tushar", 99], [15, "Sangeeta", 75]]

Push_Element(L)

Pop_Element()

Q28. Rohit has a list containing 10 integers. You need to help him create a program with
separate user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack

def Pusheven():

nlist=[]

a = eval(input("enter list of numer in list"))

for i in a[::-1]:

if i%2==0:

nlist.append(i)

print(nlist)

Pusheven()

def pop(stack):

print("Popped elements from the stack :")

while stack:

print(stack.pop())
Q29. Ankur has a list containing 10 integers. You need to help him create a
program with separate user defined functions to perform the following
operations based on this list.
● Traverse the content of the list in reverse order i.e. read the content of list
from the last(end) and push the numbers into a stack.
● Pop and display the content of the stack.

def push(stack, numbers):

for i in reversed(numbers):

stack.append(i)

def pop(stack):

print("Popped elements from the stack :")

while stack:

print(stack.pop(), end=" ")

print()

def main():

numbers = list(input("Enter 10 integers separated by spaces: "))

if len(numbers) != 10:

print("Please enter exactly 10 integers!")


stack = []

push(stack, numbers)

pop(stack)

Q30. Consider the table “student” in database “test”WAP to


display the detail of all students from the table “student” of
database “dps”.

import mysql.connector

def connect_to_db():

connection = mysql.connector.connect( host="localhost",

user="stdent",
password="dps",

database="dps" )

return connection

def display_all_students(connection):

cursor = connection.cursor()

cursor.execute("SELECT * FROM student")

results = cursor.fetchall()

print("Details of all students:")

for row in results:

print(row)

Q31. WAP to display the detail of student whose rollno is entered by the
user from the table “student” of database “test”.

def display_student_by_rollno(connection):

rollno = input("Enter the roll number: ")

cursor = connection.cursor()

cursor.execute("SELECT * FROM student WHERE rollno = %s", (rollno,))

result = cursor.fetchone()

if result:

print("Student Details:", result)

else:

print("No student found with this roll number.")


Q32. WAP to display the detail of all students who have submitted the
project from the table “student” of database “test”.

import mysql.connector as mydb

connection = mydb.connect(host = "localhost",user = "root",password = "dps123",database = "test")

cursor = connection.cursor()

def display_students_with_project(connection):

cursor = connection.cursor()

cursor.execute("SELECT * FROM student WHERE project_submitted = 1") # Assuming 1 means


"submitted"

results = cursor.fetchall()

print("Details of students who submitted the project:")

for row in results:

print(row)
Q.33 WAP to display the detail of all students who have submitted the
project from the table “student” of database “test”.
import mysql.connector as mydb

connection = mydb.connect(host = "localhost",user = "root",password = "dps123",database = "test")

cursor = connection.cursor()

def insert_new_student(connection):

rollno = input("Enter roll number: ")

name = input("Enter name: ")

marks = int(input("Enter marks: "))

section = input("Enter section: ")

project_submitted = int(input("Project submitted (1 for Yes, 0 for No): "))

cursor = connection.cursor()

query = "INSERT INTO student (rollno, name, marks, section, project_submitted) VALUES (%s, %s,
%s, %s, %s)"

cursor.execute(query, (rollno, name, marks, section, project_submitted))

connection.commit()

print("New record inserted successfully.")


Q34. WAP to display to increase the marks by 5 of all the students of
section ‘A’ from the table “student” of database “dps”.

def increase_marks_section_a(connection)

cursor = connection.cursor()

cursor.execute("UPDATE student SET marks = marks + 5 WHERE section = 'A'")

connection.commit()

print("Marks updated for students in section 'A'.")

Q35. WAP to display to remove the record of the students whose


rollno is entered by the user from the table “student” of database
“dps”.

def remove_student_by_rollno(connection):

rollno = input("Enter the roll number of the student to remove: ")

cursor = connection.cursor()

cursor.execute("DELETE FROM student WHERE rollno = %s", (rollno,))

connection.commit()

print("Record deleted successfully.")

You might also like