You are on page 1of 6

CLASS 12 : COMPUTER SCIENCE BOARD PRACTICAL EXAM 2023-24

PYTHON PROGRAMS
1.Write a program using user defined function CheckPrime(N) that takes a number N as parameter and
display the prime numbers from 2 to N
def PRIME(N): o/p
print("Prime numbers from 2 to ", N, "are") Enter the number =10
for i in range(2, N+1): Prime numbers from 2 to 10 are
for j in range(2, i): 2
if i%j == 0: 3
break 5
else: 7
print(i)
num = int(input("Enter the number ="))
PRIME(num)

2. Write a program using user defined function to read a text file and display the number of vowels ,
consonants, uppercase and lowercase characters in it.
def COUNT( ): o/p
F = open("Test1.txt", "r") The content of the file is
str = F.read( ) COMPUTER SC
print("The content of the file is") File Handling
print(str) Text Files
V =C= UC=LC=0
for i in str: No of Vowels : 10
if i.isalpha(): No of Consonants are : 21
if i.islower( ): No of Lower case letters : 17
LC +=1 No of Upper case letters : 14
elif i.isupper( ):
UC += 1
if i in 'aeiouAEIOU':
V += 1
else:
C += 1
print("No of Vowels :", V)
print("No of Consonants are :" , C)
print("No of Lower case letters :", LC)
print("No of Upper case letters :", UC)
F.close()

def WRITE():
F = open("Test1.txt","w")
L=['COMPUTER SC\n', 'File Handling \n', 'Text Files\n']
F.writelines(L)
F.close()

WRITE()
COUNT()
3.Write a program using user defined function to create a binary file with rollno and name. Search for a
given rollno and display the name, if not found display appropriate message.
import pickle o/p
def WRITE( ): WRITING RECORDS......
print("\n\nWRITING RECORDS......" ) Enter roll no:101
F=open("Bfile.dat","wb") Enter name: RAJ
while True: Do you want to enter more
rno=int(input("Enter roll no:")) records?(y/n)y
name =input("Enter name: ") Enter roll no:102
data= [rno , name] Enter name: RAVI
pickle.dump(data, F) Do you want to enter more
ch = input("Do you want to enter more records?(y/n)") records?(y/n)y
if ch in 'nN': Enter roll no:103
break Enter name: MOHAN
F.close( ) Do you want to enter more
records?(y/n)y
def SEARCH( ): Enter roll no:104
print("\n\nSEARCHING A RECORD....." ) Enter name: JACK
F=open("Bfile.dat","rb") Do you want to enter more
rno = int(input("Enter roll no:")) records?(y/n)n
found =0
try:
while True: SEARCHING A RECORD.....
S = pickle.load(F) Enter roll no:103
if S[0] == rno: RECORD FOUND……
print("RECORD FOUND……") NAME = MOHAN
print("NAME =", S[1])
found = 1
except EOFError:
F.close()
if found == 0:
print("Record not found….")
WRITE()
SEARCH()

4. Write a program using user defined function to create a CSV file by entering userid and password.
Display the password for given userid .
import csv o/p
def WRITE(): Enter user id:RAJ12
F= open("User.csv","w",newline="") Enter password:R101
W=csv.writer(F) Do you want to enter more records ?(y/n)y
W.writerow(["USERID", "PASSWORD"]) Enter user id:RAVI12
while True: Enter password:R105
uid=input("Enter user id") Do you want to enter more records ?(y/n)y
pword=input("Enter password") Enter user id:MOHAN12
data=[uid, pword] Enter password:M221
W.writerow(data) Do you want to enter more records ?(y/n)y
ch = input("Do you want to enter more records ?(y/n)") Enter user id:HARI12
if ch in "nN": Enter password:H431
break Do you want to enter more records ?(y/n)n
F.close( ) Enter user id to search: MOHAN12
PASSWORD= M221
def SEARCH( ):
F =open("User.csv", "r")
found = 0
uid= input("Enter user id to search")
R = csv.reader(F)
next(R)
for i in R:
if i[0] == uid:
print("PASSWORD=",i[1])
found = 1
if found == 0:
print("No record found")
F.close()

WRITE()
SEARCH()

5.Write a Python program to implement a stack using list.


stack =[]
while True:
print("1.PUSH 2.POP 3.Display elements of stack")
choice=int(input("Enter your choice"))
if choice == 1:
a=int(input("Enter the element which you want to push"))
stack.append(a)
elif choice == 2:
if stack == []:
print("Stack is empty...Underflow case...")
else:
print("Deleted element is :", stack.pop())
elif choice == 3:
print("The elements in the stack =", stack)
else:
print("Wrong input...")
ch = input("Do you want to continue or not(y/n) ?")
if ch in 'nN':
break

o/p
1.PUSH 2.POP 3.Display elements of stack
Enter your choice1
Enter the element which you want to push10
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice1
Enter the element which you want to push20
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice1
Enter the element which you want to push30
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice3
The elements in the stack = [10, 20, 30]
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice2
Deleted element is : 30
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice3
The elements in the stack = [10, 20]
Do you want to continue or not(y/n) ?n

DATABASE MANAGEMENT
1)Write the sql queries for the following questions:
i)Create the following table Student:

ii)Insert the following information to the table student:


111, ‘Anu Jain’ , 12, ‘A’, 2500
iii)Display the details of the students in the ascending order of fees
iv)Display the number of students in each class
Ans)
i)CREATE TABLE Student
( Adno Numeric(3) Primary key,
Name Varchar(20) NOT NULL,
Class Numeric(2) ,
Section Char(1),
Fees Numeric(10,2)
);
ii)INSERT INTO Student VALUES (111, ‘Anu Jain’, 12, ‘A’, 2500)
iii)SELECT * FROM Student ORDER BY Fees;
iv) SELECT Class, COUNT(*) FROM Student GROUP BY Class;

2)Write the sql queries for the following questions:


Table : Student
i)Display name of 10th class student information.
ii)Display students’ name , who are paying above or equal to 3000 fees.
iii)Display the names that start with letter ‘A’.
iv)Display sum of fees for each class.
Ans)
i)SELECT Name FROM Student WHERE Class = 10;
ii)SELECT Name FROM Student WHERE Fees >= 3000;
iii)SELECT Name FROM Student WHERE Name LIKE “A%” ;
iv)SELECT Class ,SUM(Fees) FROM Student GROUP BY Class;

3)Write the sql queries for the following questions:


Table : Student

i)Display students’ information , who are paying fees between 2500 and 3500
ii)Remove adno 444 information.
iii)Increase the fees value by 100 for adno 222
iv)Display lowest and highest fees.
Ans)
i)SELECT * FROM Student WHERE Fees BETWEEN 2500 AND 3500;
ii)DELETE FROM Student WHERE Adno = 444;
iii)UPDATE Student SET Fees = Fees + 100 WHERE Adno =222;
iv)SELECT MIN(Fees) , MAX(Fees) FROM Student;

4)Write the sql queries for the following questions:


Table : Student

i)Display students’ information , who are in section A and B.


ii)Display information of students in class 11B.
iii)Display the details of the students in descending order of fees.
iv)Display the sum and average of fees.
Ans)
i)SELECT * FROM Student WHERE Section =’A’ OR Section = ‘B’;
ii)SELECT * FROM Student WHERE Class = 11 AND Section = ‘B’;
iii)SELECT * FROM student ORDER BY Fees DESC;
iv)SELECT SUM(Fees) , AVG(Fees) FROM Student;

5)Write the sql queries for the following questions:


Table : Student
i)Display 11th and 12th class students’ information.
ii)Display sum of fees for each class.
iii)Display different classes from student table.
iv)Increase the fees value by 100 for adno 222.
Ans)
i)SELECT * FROM Student WHERE Class = 11 OR Class = 12;
ii)SELECT Class, SUM(Fees) FROM Student GROUP BY Class;
iii)SELECT DISTINCT Class FROM Student;
iv)UPDATE Student SET Fees = Fees + 100 WHERE Adno = 222;

6.Write the sql queries for the following questions:

Ans)
i)SELECT INAME, PRICE, BNAME
FROM ITEM I , BRAND B
WHERE I.ICODE = B.ICODE AND PRICE BETWEEN 25000 AND 30000;
ii)SELECT ICODE , PRICE , BNAME
FROM ITEM I , BRAND B
WHERE I.ICODE = B.ICODE AND INAME = “Television”;
iii)UPDATE ITEM SET PRICE = PRICE + PRICE *0.1 ;

You might also like