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

Dhruv Sharma CS Practical File

Uploaded by

ashjoddd7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Dhruv Sharma CS Practical File

Uploaded by

ashjoddd7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

DARBARI LAL DAV MODEL SCHOOL

SHALIMAR BAGH

COMPUTER SCIENCE
PRACTICAL FILE
2024-2025

NAME - DHRUV SHARMA


CLASS - XII - B
ROLL NO. 13

1
INDEX PAGE

1.USER DEFINED FUNCTION QUESTIONS PAGE NO. - 3 to 10


2. TEXT FILE QUESTIONS PAGE NO. - 10 to 16
3. BINARY FILE QUESTIONS PAGE NO. - 17 to 22
4.CSV FILE PAGE NO - 23 to 30
5.STACKS PAGE NO -.31 to 34
6.MYSQL ASSIGNMENT-1 PAGE NO - 35 to 44
7.MYSQL ASSIGNMENT-2 PAGE NO - 44 to 59
8. MYSQL ASSIGNMENT-3 PAGE NO - 59 to 68
9. MYSQL ASSIGNMENT-4 PAGE NO - 69 to 73
9.MYSQL ASSIGNMENT-5 PAGE NO - 74 to 80
10.Python-MySQL Connectivity PAGE NO - 80 to 89

2
TOPIC-1
USER DEFINED FUNCTION
QUESTIONS

QUES - 1 Write a program using a user defined function calcFact() to calculate


and display the factorial of a number num passed as an argument.

SOLUTION -

OUTPUT -

3
QUES - 2 Write a program using a user defined function myMean()
to calculate the mean of floating values stored in a list.

SOLUTION -

OUTPUT -

4
QUES - 3 Write a program using a user defined function that accepts
an integer and increments the value by 5. Also display the id of
argument (before function call), id of parameter
before increment and after increment.

SOLUTION -

OUTPUT -

5
QUES - 4 Write a program using a user defined function that displays
the sum of first n natural numbers, where n is passed as an
argument.

SOLUTION -

OUTPUT -

6
QUES - 5The company performs the following tasks to fix the selling price of
each tent.
1. Accept user requirements for the tent, such as
a) height
b) radius
c) slant height of the conical part
2. Calculate the area of the canvas used
3. Calculate the cost of the canvas used for making the tent
4. Calculate the net payable amount by the customer that is
inclusive of the 18% tax. Write separate user defined functions for
part 2,3 and 4.

SOLUTION -

7
Output-

8
QUES - 6 Write a program that simulates a traffic light. The program should consist of the
following:
1. A user defined function trafficLight( ) that accepts input from the user, displays an error
message if the user enters anything other than RED, YELLOW, and GREEN. Function light() is
called and following is displayed depending upon return value from light().
a) “STOP, your life is precious” if the value returned by light() is 0.
b) “Please WAIT, till the light is Green“ if the value returned by light() is 1
c) “GO! Thank you for being patient” if the value returned by light() is 2.
2. A user defined function light() that accepts a string as input and returns 0 when the input is
RED, 1 when the input is YELLOW and 2 when the input is GREEN. The input should be passed as
an argument.
3. Display “SPEED THRILLS BUT KILLS” after the function trafficLight( ) is executed.

SOLUTION -

OUTPUT -

9
QUES - 7 Create a user defined module basic_math that contains the
following user defined functions:
1. To add two numbers and return their sum.
2. To subtract two numbers and return their difference.
3. To multiply two numbers and return their product.
4. To divide two numbers and return their quotient and print “Division by
Zero” error if the denominator is zero.
Also add a docstring to describe the module. After creating the module,
import and execute functions at shell prompt.

SOLUTION -

OUTPUT -

10
TOPIC-2
TEXT FILE
QUESTIONS

QUES - 8 Write a function DISPLAYWORDS() in Python to read lines from a text


file ‘Story.txt’ and display those words, which are less than 4 characters.

SOLUTION -

OUTPUT -

11
QUES - 9 Write a function in Python to read a text file line by line and
display each word separated by #.

SOLUTION -

OUTPUT -

12
QUES - 10 Write a function in Python to read a text file and display the number
of vowels/ consonants/ uppercase / lowercase characters in a file.
SOLUTION -

OUTPUT -

13
QUES - 11 Write a program in python to Reverse and display each line
of text.
SOLUTION -

OUTPUT -

14
QUES - 12 Write a program in python to display lines of text ending with ‘Y’
or ‘y’.
SOLUTION -

OUTPUT -

15
QUES - 13 Write a function in Python to read a text file and convert the
uppercase letter into lowercase and vice versa.

SOLUTION -

OUTPUT -

16
TOPIC-3
BINARY FILE
QUESTIONS

QUES - 14 Write a function in PYTHON to search for a BookNo from a binary


file “BOOK.DAT”, after creating the binary containing the records of the following
type: {BookNo":value, "Book_name":value} The BookNo is an
integer.
SOLUTION -

OUTPUT -

17
QUES - 15 Assuming that a binary file VINTAGE.DAT contains records of the
following type, {VNO, VDesc, price} write a function in PYTHON to read the data
VINTAGE.DAT and display those vintage vehicles, which are priced between 200000 and
250000.
SOLUTION -

OUTPUT -

18
QUES - 16 Write a function in Python to search for a laptop from a binary file
“laptop.dat” containing records of following type [ ModelNo, RAM, HDD, Details] Where
ModelNo, RAM, HDD are integers and Details is a string.The user should enter the model
number and the function should display the details of the laptop.
SOLUTION -

OUTPUT -

Ques-17 Write a function in Python to search for the details of those mobile phones
which have more than 1000 calls from a binary file “mobile.dat”. Assume that binary file
contains records of the following type: (Number, Calls)
SOLUTION -

19
OUTPUT -

Ques-18 Write a function in Python to read each record of a binary file “items.dat”, find
and display those items which cost less than 2500. Assume that the file is created with
the help of objects of the following type:
{ ID:string, GIFT:string, Cost:integer}
SOLUTION -

20
OUTPUT -

Ques-19 Write a function bumper() in Python to read each object of a binary file
“gifts.dat”, find and display details of those gifts, which have remarks as “on discount”.
Assume the file is created with the help of lists of following type:
[ID, Gift, Remarks, Price]
SOLUTION -

21
OUTPUT -

22
TOPIC-4
CSV FILE
QUESTIONS

Ques-20 A CSV file TEACHER.CSV has following data:


TCODE,TNAME,SUBJECT,DESIG,NOP
1001,ADITI JAIN,PHYS,HOD,20
Write an interactive menu driven to do the following (using CSV module):
● Append following records(lines) in the file TEACHER.CSV
1011,MANOJ KUMAR,ENG,PGT,24
1012,NAMITA KAUR,ENG,TGT,28
1013,PRATAP SHINDE,ENG,TGT,29
1014,GUARI KAPOOR,ENG,PRT,30
1015,HITESH DESAI,MATH,HOD,20
1016,RAKESH YADAV,MATH,PGT,26
1017,ISHITA DASH,MATH,TGT,29
1018,DILIP MATHUR,MATH,TGT,27
1019,NUPUR CHACKO,MATH,PRT,30
1020,MANAS KHER,PHYS,PRT,30
● Read and display the CSV file; at the end display number of records(lines) present in
the file
● Read the CSV file and display details if DESIG is HOD; at the end display number of
such records (lines) found in the file
● Read the CSV file and display details if DESIG is PGT and SUBJECT is either PHYS or
CHEM; if no record found in the CSV file then display a message 'Record not found in the
file'
● Exit from the menu and quit the program

SOLUTION -

23
24
25
OUTPUT -

26
27
Ques-21 A CSV file TEACHER.CSV has following data:
CODE,NAME,SUBJECT,DESIG,NOP
1001,ADITI JAIN,PHYS,HOD,20
Write an interactive menu driven to do the following (using CSV module):
● Read the CSV file and display details of teachers whose DESIG is either TGT or PGT
and SUBJECT is ENG; if no record is found then display a message 'Record not found in
the file'
● Read the CSV file and display details if DESIG is either PRT or TGT; at the end display
number of such records found in the file
● Exit from the menu and quit the program
SOLUTION -

28
OUTPUT -

29
30
TOPIC -5
STACKS
QUESTIONS

Ques-22 Write a function in Python that reverses a string using Stack


data structures.

SOLUTION -

OUTPUT -

31
Ques-23 Write functions AddPlayer(player) and DeletePlayer(player) in python to
add and remove a player by considering them as push and pop operations in a
stack.

SOLUTION -

OUTPUT -

32
Ques-24 Monika has created a dictionary containing names and marks as key-value
pairs of 5 students.
Write a program, with separate user-defined functions to perform the following
operations:
1. Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value
(marks) is greater than 70.
2. Pop the content of the stack
3. Display the content of the stack.
SOLUTION -

33
OUTPUT -

34
TOPIC-6
MYSQL
ASSIGNMENT -1

Q-1 Write SQL Command for (a) to (o) and output of (p)

TABLE : GRADUATE

S.NO NAME STIPEND SUBJECT AVERAGE DIVISION

1 KARAN 400 PHYSICS 68 I


2 DIWAKAR 450 COMP Sc 68 I
3 DIVYA 300 CHEMISTRY 62 I
4 REKHA 350 PHYSICS 63 I
5 ARJUN 500 MATHS 70 I
6 SABINA 400 CHEMISTRY 55 II
7 JOHN 250 PHYSICS 64 I
8 ROBERT 450 MATHS 68 I
9 RUBINA 500 COMP Sc 62 I
10 VIKAS 400 MATHS 57 II

35
a. Create Database DB1 and Create Table Graduate in DB1 database.

SOLUTION-

b. Insert 10 rows in Graduate table.


SOLUTION-

36
c. List the names of those students who have obtained DIV I.

37
SOLUTION-

d. Display a report, listing NAME, STIPEND, SUBJECT and amount of


stipend received in a year assuming that the STIPEND is paid every
month.
SOLUTION-

e. To display students details who are studying maths.


SOLUTION-

38
f.To insert a new 11th row in the GRADUATE table:(11,”KAJOL”, 300,
“Comp Sc”, 75, 1)
SOLUTION-

g. Add a new column GRADE of character type.


SOLUTION-

h.Display Name and division only in Graduate table.


SOLUTION-

39
i.To display students details sorted by NAME.

SOLUTION-

j.To display students details sorted by NAME in descending order.

SOLUTION-

40
k.To display student’s details whose average is between 50 to 60.

SOLUTION-

l.Display student RUBINA’s details.

SOLUTION-

m.Delete KAJOL’s details from table.


.

41
n.To display students details who are either PHYSICS or COMPUTER SC
graduates.

o.Give the output of following sql statement based on table GRADUATE:


(i) Select * from GRADUATE where SUBJECT=”PHYSICS”;

42
(ii) Select Name, Average from GRADUATE WHERE div=2;

(iii) Select from Name, Average from GRADUATE where AVERAGE>=65;

(iv) Select distinct SUBJECT from GRADUATE;

43
44
MYSQL
ASSIGNMENT-2

1. Consider a database LOANS with the following table:

Table: Loan_Accounts

AccNo Cust_Name Loan_Amount Instalments Int_Rate Start_Date Interest

1 R. K. Gupta 300000 36 12.00 19-07-2007

2 S. P. Sharma 500000 48 10.00 22-03-2008

3 K. P. Jain 300000 36 NULL 08-03-2007

4 M. P. Yadav 800000 60 10.00 06-12-2008

5 S. P. Sinha 200000 36 12.50 03-01-2010

6 P. Sharma 700000 60 12.50 05-06-2008

7 K. S. Dhall 500000 48 NULL 05-03-2008

Write SQL commands for the tasks 1 to 35


Create Database and use it
1. Create the database LOANS.
2. Use the database LOANS.
SOLUTION-

45
B.Create Table / Insert Into-

3. Create the table Loan_Accounts and insert tuples in it.

C.) Simple Select

46
4. Display the details of all the loans.

5. Display the AccNo, Cust_Name, and Loan_Amount of all the


loans.

D.) Conditional Select using Where Clause-

47
6. Display the details of all the loans with less than 40
installments.

7. Display the AccNo and Loan_Amount of all the loans started


before 01-04-2009.

8. Display the Int_Rate of all the loans started after 01-04-2009.

48
E) Using NULL-

9. Display the details of all the loans whose rate of interest is


NULL.

10. Display the details of all the loans whose rate of interest is
not NULL.

49
F.) Using DISTINCT Clause-

11. Display the amounts of various loans from the table


Loan_Accounts. A loan amount should appear only once.

12. Display the number of installments of various loans from the


table Loan_Accounts. An installment should appear only once.

G.) Using Logical Operators (NOT, AND, OR)-

50
13. Display the details of all the loans started after 31-12-2008
for which the number of installments are more than 36.

14. Display the Cust_Name and Loan_Amount for all the loans
which do not have 36 installments.

15. Display the Cust_Name and Loan_Amount for all the loans
for which the loan amount is less than 500000 or int_rate is more
than 12.

51
16. Display the details of all the loans which started in the year
2009.

17. Display the details of all the loans whose Loan_Amount is in


the range 400000 to500000.

18. Display the details of all the loans whose rate of interest is in
the range 11% to 12%.

H.) Using IN Operator

52
19. Display the Cust_Name and Loan_Amount for all the loans
for which the number of installments are 24, 36, or 48. (Using IN
operator)

I.) Using BETWEEN Operator

20. Display the details of all the loans whose Loan_Amount is in


the range 400000 to 500000. (Using BETWEEN operator)

21. Display the details of all the loans whose rate of interest is in
the range 11% to 12%.(Using BETWEEN operator)

53
J.) Using LIKE Operator

22. Display the AccNo, Cust_Name, and Loan_Amount for all the
loans for which theCust_Name ends with ‘Sharma’.

23. Display the AccNo, Cust_Name, and Loan_Amount for all the
loans for which the Cust_Name ends with ‘a’.

24. Display the AccNo, Cust_Name, and Loan_Amount for all the
loans for which the Cust_Name contains ‘a’.

54
25. Display the AccNo, Cust_Name, and Loan_Amount for all the
loans for which the Cust_Name does not contain ‘P’.

26. Display the AccNo, Cust_Name, and Loan_Amount for all the
loans for which the Cust_Name contains ‘a’ as the second last
character.

K.) Using ORDER BY clause

55
27. Display the details of all the loans in the ascending order of
their Loan_Amount.

28. Display the details of all the loans in the descending order of
their Start_Date.

29. Display the details of all the loans in the ascending order of
their Loan_Amount and within Loan_Amount in the descending
order of their Start_Date.

56
L. Using UPDATE, DELETE, ALTER TABLE

30. Put the interest rate 11.50% for all the loans for which
interest rate is NULL

31. Increase the interest rate by 0.5% for all the loans for which
the loan amount is more than 400000.

57
32. For each loan compute Interest as
(Loan_Amount*(Int_Rate/100)*Installments).

33. Delete the records of all the loans whose start date is before
2007.

34. Delete the records of all the loans of ‘K.P. Jain’;

58
35. Add another column Category of type CHAR(1) in the Loan
table.

59
MYSQL
ASSIGNMENT -3

Q1. Create Database Work and activate it.

Table:Worker

EMPNO ENAME JOB HIREDATE SAL COMM DEPTNO

8369 SMITH CLERK 1990-12-18 8000 NULL 20

8499 ANYA SALESMAN 1991-02-20 16000 500 30

8521 MAHADEVAN SALESMAN 1991-02-22 10000 300 30

8566 MOMIN MANAGER 1991-04-02 25000 NULL 10

8654 BINA ANALYST 1992-12-09 30000 NULL 20

8698 SHIAVNSH PRESIDENT 1991-11-18 50000 NULL 10

8882 SCOTT SALESMAN 1991-05-01 12000 1200 30

60
2. Create the table WORKER and add records as shown below. Write SQL

commands for the statements (a) to (g).

a. To display the details in ascending order of salary.

b. To display empno, empname in descending order of hiredate.

61
c. To display the details in ascending order of empno.

d. To display the sum of salary with heading ‘Sum of salary’.

e. To display the number of employees in each department


number.

62
f. To display the number of employees in each department but
the number of employees should be less than 3.

g. To display the sum of salary, average of salary, maximum and


minimum salary of each job title.

63
3. Create a table EMPLOYEE with the following data in the Work database.
Write SQL commands for the statements (a) to (h).

Table: EMPLOYEE

No Name Salary Area Age Grade Dept

711 KESHAR 40000 WEST 45 C CIVIL

262 KIRTI 35000 SOUTH 38 A ELEC

693 KRIPPLE 60000 NORTH 52 B CIVIL

724 ARYAN 38000 NORTH 29 B CIVIL

705 SAMSON 42000 EAST 35 A COMP

606 BISWAL 29000 SOUTH 34 A MECH

a. To display the list of all employees in ascending order of


salary.

64
b. To display the employee names in descending order of age.

c. To display the grade and number of employees in each grade.

65
d. To display the area and number of employees in each area.

e. To display the sum and average salary of the employees of


grade ‘B’ .

66
f. To display grade and maximum, minimum and average salary
of each grade.

g. To display the sum, average, maximum and minimum of


salary.

h. To display the grade and the number of employees in each


grade where the number of employees are more than two.

67
68
MYSQL
ASSIGNMENT -4

4.Create Database Cust and create two tables HANDSETS and


CUSTOMER in it as given below:

SetCode SetName TouchScreen Phone Cost


N1 Nokia 2G N 5000
N2 Nokia 3G Y 8000
B1 BlackBerry 2G N 14000

CustNo SetNo CustAddress

1 N2 Delhi

2 B1 Mumbai

3 N2 Mumbai

4 N1 Kolkata

5 B1 Delhi

With reference to these tables, Write commands in SQL for (a)


and (b) and output for (c) below:
a. Display the CustNo, CustAddress and corresponding SetName

69
for each customer.

b. Display the Customer Details for each customer who uses a Nokia
handset.

c. select SetNo, SetName from Handsets, customer where SetNo =


SetCode and CustAddress = 'Delhi';

5. Create the following tables SENDER and RECIPIENT in Cust database. Write
SQLcommands for the statements (i) to (iv) and give outputs for SQL queries (v) to
(viii)

Table:Sender

70
SenderID SenderName SenderAddress SenderCity

ND01 R Jain 2, ABC Appts New Delhi

MU02 H Sinha 12, Newtown Mumbai

MU15 S Jha 27/A, Park Street New Delhi

ND50 T Prasad 122 K, SDA Mumbai

Table:Recipient

RecID SenderID RecName RecAddress RecCity

K005 ND01 R Bajpayee 5, Central Avenue Kolkata

ND08 MU02 S Mahajan 116, A Vihar New Delhi

MU19 ND01 H Singh 2A, Andheri East Mumbai

MU32 MU15 P K Swamy B5, C S Terminus Mumbai

ND48 ND50 S Tripathi 13, Mayur Vihar New Delhi

a. Display the names of all senders from MUMBAI

b. Display the RecID, SenderName, SenderAddress, RecName, RecAddress


for every recipient.

71
c. Display the recipient details in ascending order of RecName.

d. Display the number of recipients in each city.

GIVE OUTPUTS

e. SELECT DISTINCT SenderCity FROM sender;

72
f. SELECT A.SenderName, B.RecName FROM Sender A, Recipient B WHERE A.SenderID
= B. SenderID AND B.RecCity = ‘Mumbai’;

g. SELECT RecName, RecAddress FROM Recipient WHERE RecCity NOT IN


(‘Mumbai’,’Kolkata’);

h. SELECT RecID, RecName FROM Recipient WHERE SenderID =


‘MU02’ OR SenderID = ‘ND50’;

73
MYSQL
ASSIGNMENT -5

Q6(I).Create the following tables PRODUCT and CLIENT. Write SQL


commands for the statements (a) to (d) and give outputs for SQL queries (e) to
(h)
Table: PRODUCT

P_ID ProductName Manufacturer Price

TP01 Talcum Powder LAK 40

FW05 Face Wash ABC 45

BS01 Bath Soap ABC 55

SH06 Shampoo XYZ 120

FW12 Face Wash XYZ 95

Table: CLIENT

C_ID ClientName City P_ID

1 Cosmetic Shop Delhi FW05

6 Total Health Mumbai BS01

12 Live Life Delhi SH06

15 Pretty One Delhi FW12

16 Dreams Bangalore TP01

i. To display the details of those clients whose city is Delhi.

74
j. To display the details of the products whose price is in the range 50 to 100 (Both
values included).

k. To display the ClientName, City from table Client, and ProductName and Price
from table Product, with their corresponding matching P_ID.

l. To increase the price of all products by 10.

75
m. SELECT DISTINCT City FROM Client;

n. SELECT Manufacturer , MAX(price) , MIN(Price), COUNT(*)FROMProduct


GROUP BY Manufacturer;

76
o. SELECT ClientName , Manufacturer FROM Product P, Client C WHERE
C.P_ID = P.P_ID;

p. SELECT ProductName, Price * 4 FROM Product; WHERE P_ID =


‘TP01’ OR P_ID = ‘SH06’;

6. (II) Create the following tables WORKER and PAYLEVEL. Write SQL commands for
the statements (a) to (d) and give outputs for SQL queries (e) to (h)

Table:- WORKER

77
ECODE NAME DESIG PLEVE DOJ DOB
L

11 Radhe Shyam Superviso P001 13-Sep-2004 23-Aug-1981


r

12 Chander Nath Operator P003 22-Feb-2010 12-Jul-1987

13 Fizza Operator P003 14-Jun-2009 14-Oct-1983

15 Ameen Mechanic P002 21-Aug-2006 13-Mar-1984


Ahmed

18 Sanya Clerk P002 19-Dec-2005 09-Jun-1983

Table: PAYLEVEL
PLEVE PAY ALLOWANCE
L

P001 26000 12000

P002 22000 10000

P003 12000 6000

a. To display the details of all WORKERs in descending order of DOB

78
b. To display NAME and DESIG of those WORKERs, whose PLEVEL is either P001
or P002.

c. To display the content of all the WORKERs, whose DOB is in between ’19-JAN-1984’
and ’18-JAN-1987’.

d. To add a new row with the following:


19, ‘Daya Kishore’, ‘Operator’, ‘P003’, ’19-Jun-2008’, ’11-Jul-1984’

79
e. SELECT COUNT(PLEVEL), PLEVEL FROM WORKER GROUP BYPLEVEL;

g. SELECT Name, Pay FROM WORKER W, PAYLEVEL P WHERE W.PLEVEL = S.


PLEVEL AND P.ECODE < 13;

h. SELECT PLEVEL, PAY + ALLOWANCE FROM PAYLEVEL WHERE PLEVEL


= ‘P003’;

80
Practical Assignment -6
(Python-MySQL Connectivity)

Integrate SQL with Python by importing


MySQLdb/mysql.connector

1. Create a table Student in a database ‘project’ as shown below:


RollNo int primary key
Name varchar(20)
Total int
Grade varchar(3)
a. Input Rollno, Name, Total marks and Grade and insert the record in a table Student.
b. Input Rollno and update the corresponding record in a table Student with the value
entered by the user.
c. Display all the records from a table Student.

81
82
83
84
Output-

2. Consider the table Product with following structure.


P_ID | ProductName | Manufacturer | Price
1 | Laptop | Dell | 1000.0
2 | Phone | Apple | 800.0
3 | Tablet | Samsung | 600.0

85
a.Write Python code to increase the price of all products by 50.

86
3.Create a table Student in a database ‘project’ as shown below:
RollNo int primary key
Name varchar(20)
Total int
Grade varchar(3)

a. Input Rollno and display the corresponding information from the table
Student.

b. Input Rollno and delete the corresponding record from the table Student.

87
c. Display all the records of a grade entered by the user from the table
Student.

d. Display all the records in descending order of total marks from the table
Student.

88
89

You might also like