Computer Practical Class 12
Computer Practical Class 12
SNO Program
1. WAP to generate any 5 random numbers from the range 1-100.
2. WAP to generate 3 random integers between 10 and 99 which are divisible by 7.
3. WAP to generate a 6 digit random OTP.
4. Write the code to pick a random character from a string.
WAF push() which accepts a parameter package name. It should add the package name
5.
in stack.
WAP to create a file “notes.txt” containing the following content.
6.
“Python is an open-source programming language.”
7. WAP to count the number of words in the text file “notes.txt”
8. WAP to print a random phone number
9. WAP to select a lottery winner of ticket no. btw 1000000000,10000000000
write a function to 1) print the name of person who is living in a deluxe room and 2)
10.
pop the last argument.
11. Write a program to read data from “student.dat”
12. Write a program to read a string and find the sum of all digits present in the string
13. Create a csv file book.csv and enter record of the books user wants
14. Write a code to count number of books in file “book.csv”.
W.A.P to create a csv file “employee.csv” which contains employee no., name, city,
15.
designation and salary and ask the data from user.
Create a file user.csv which contains names and passwords of different users, using a
16.
function add user which will add single user at a time.
Write a code to read user.csv and find out the password of a given user. Use the
17.
function findpassword()
18. WAF checkprime() to check whether the number is prime or not.
Write a program to input the salary and calculate tax to be paid according to the given
19.
slab.
WAP to arrange string char. In such a way that lowercase letters come first the
20.
uppercase then any special char. Or string
21. WAP to read a string and find the sum of all digits in the string.
Write a menu driven program to calculate: 1. Area of circle[A=πr2] 2. Area of square
22.
[A=a*a]
23. WAP to check if the entered email is valid.
24.
My SQL programs
25.
My SQL programs
26. My SQL programs
Write a program to insert a record in the table student using python statements. Data
27.
should be read from user.
28. Write a program to create a file ‘notes.txt’ containing the following content
29. Write a program to read marks of 10 students and save it in a file – marks.txt’ .
30. Write a program to count the number of words in the text file ‘notes.txt’ .
Write a program that copies the content of file- ‘source.txt’ onto ‘target.txt’ by
31.
removing all the lines starting with ‘a’.
32. Write a program to calculate the number of lines and size of text file ‘text.txt’.
33. Write a program to add these two line to text file- ‘student.txt’ :
1)welcome to school
2)you will learn more hear
34. Consider a file ‘student.txt’ and find out:1) the names which start from ‘h’. 2) the
number of times ‘is’ occur.
Write a program to read a text file and display the number of vowels/ consonants/
35.
uppercase/ lowercase characters in the file.
36. Write a program to create a binary file- ‘list.dat’ .
Write a program to read roll number and name of a student and store it in a binary file.
37.
Read the record of ‘n’ number of students. Also display the name of a given roll number.
38. Write a program to create a binary file- ‘efile.dat’ which contains number, name, basic
salary, and allowance for ‘n’ number of employees. Also display total salary of each
employee
39. A binary file ‘customer.dat’ has the structure: [id, customer name, category]. Write a
program to read and display the details of a customer whose category is ‘g’.
A binary file ‘fruit.dat’ has the structure: [fruit id, fruit name, cost]. Write a program to
40.
read each record and display the names of those fruits whose cost is more than 200.
Write a program to create a binary file- ‘student.dat’ which contains student name, roll
41.
number, class, marks, city.
Write a program to create a csv file ‘emp.csv’ which contains employee number, name,
42.
designation, salary and city. Ask the data from the user.
Create a file ‘user.csv’ which contains a username and password from different users
43.
using a function ‘add ()’ which will add one user at a time.
Write the code to read ‘user.csv’ and find the password of a given user. Use function
44.
‘findpassword()’.
45. Create a csv file ‘book.csv’. Write the user defined function- ‘create.file()’ to input to
record of book, ‘countbooks()’ to count number of books in file. The record of the book
includes book name, author and price.
46. My SQL programs
Write a python program to display the name of students who have scored more than 90
47.
marks.
48. Write a python program to change marks of a student for the given roll number.
Write a python program to insert a record in the table ‘student’. data should be read
49.
from the user.
50. Write a python program to display all the details of students for a given roll number.
Computer practical
Jayant Sharda Class 12-B
Program 1
WAP to generate any 5 random numbers from the range 1-100.
import random
for i in range(5):
print(random.randint(1,100))
Program 2
WAP to generate 3 random integers between 10 and 99 which are divisible by 7.
import random
for i in range(3):
print(random.randrange(10,100,7))
Program 3
WAP to generate a 6 digit random OTP.
import random
a=' '
for i in range(6):
a+=str(random.randint(0,10))
print("your OTP is",a)
Program 4
Write the code to pick a random character from a string.
import random
n=input("enter a string")
print(random.choice(n))
Program 5
WAF push() which accepts a parameter package name. It should add the package name in
stack.
l=[]
def push(s,p):
s.append(p)
def pop(s):
if s==[]:
print("stack is empty")
else:
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
for i in range(3):
a=input("enter package name")
push(l,a)
pop(l)
Program 6
WAP to create a file “notes.txt” containing the following content.
“Python is an open source programming language.”
f=open("notes.txt","w")
f.write("Python is an\n")
f.write("open source\n")
f.write("programing language.")
f.close()
Program 7
WAP to count the number of words in the text file “notes.txt”
f=open("notes.txt","r")
s=f.read()
l=s.split()
print("no of words",len(l))
f.close()
Program 8
WAP to print a random phone number
import random
c=""
for i in range(10):
a=str(random.randint(0,9))
c=c+a
print(c)
Program 9
WAP to select a lottery winner of ticket no. btw 1000000000,10000000000
import random
for i in range(3):
print(random.randrange(100 0000000,10000000001))
Program 10
write a function to 1) print the name of person who is living in a deluxe room and 2) pop the
last argument.
l=[["jayant","deluxe"],["jaspreet","standard"],
["harshit","deluxe"]]
def push():
for i in l:
if i[1]=="deluxe":
print(i)
def Pop():
if l==[]:
print("underflow condition")
else:
print(l.pop())
push()
Pop()
Program 11
Write a program to create a binary file called “student.dat” which contains the following
information about student:
Student name, Roll no., Class, Marks, City
import pickle
f=open("student.dat",'wb')
l={}
while True:
n=input("enter student's name : ")
rn=int(input("enter roll number : "))
c=int(input("enter class : "))
m=int(input("enter marks : "))
ci=input("enter city : ")
d=[]
d.append(rn)
d.append(c)
d.append(m)
d.append(ci)
l[n]=d
a=input("do you want to enter more records: ")
if a!="y" :
break
pickle.dump(l,f)
f.close()
Program 12
Write a program to read data from “student.dat”
import pickle.
f=open("student.dat",'rb')
d=pickle.load(f)
for i in d:
print("DETAILS OF",i)
print("Name : ",i)
print("Roll number : ",d[i][0])
print("Class : ",d[i][1])
print("Marks : ",d[i][2])
print("City: ",d[i][3])
f.close()
Program 13
Create a csv file book.csv and enter record of the books user wants.
import csv
f=open("book.csv",'a', newline=””)
w=csv.writer(f)
n=int(input("Number of books want to add:"))
for i in range(n):
l=[]
m=int(input("Book number:"))
o=input("Book name:")
p=int(input("Book price:"))
l=[m,o,p]
w.writerow(l)
f.close()
Program 14
Write a code to count number of books in file “book.csv”.
import csv
f=open("book.csv",'r')
r=csv.reader(f)
c=0
for i in r:
c=c+1
print("Number of books:",c)
f.close()
Program 15
W.A.P to create a csv file “employee.csv” which contains employee no., name, city,
designation and salary and ask the data from user.
import csv
f=open("emp.csv",'w',newline='')
w=csv.writer(f)
h=["eno","ename","designation","salary","city"]
w.writerow(h)
while True:
try:
l=[]
eno=int(input("enter employee number: "))
en=input("enter name: ")
d=input("enter designation: ")
s=int(input("enter salary: "))
c=input("enter city: ")
l=[eno,en,d,s,c]
w.writerow(l)
a=input("Do you want to continue(y/n): ")
if a=="y" or a=="Y":
continue
else:
break
except:
print("Enter valid details!")
continue
f.close()
Program 16
Create a file user.csv which contains names and passwords of different users, using a
function add user which will add single user at a time.
import csv
f=open("user.csv",'a')
def adduser():
n=input("Enter name:")
p=input("Enter password:")
l=[]
l=[n,p]
w=csv.writer(f)
w.writerow(l)
while True:
a=input("Add password?(Y/N):")
if a=="Y":
adduser()
else:
break
f.close()
Program 17
Write a code to read user.csv and find out the password of a given user. Use the function
findpassword()
import csv
f=open("user.csv",'r')
def findpassword():
n=input("Enter name:")
r=csv.reader(f)
for i in r:
if i[0]==n:
print(i[1])
break
while True:
a=input("Find password(Y/N):")
if a=="Y":
findpassword()
else:
break
Program 18
WAF checkprime() to check whether the number is prime or not.
n=int(input("enter a number"))
def checkprime(n):
for i in range(2,n):
if n%i==0:
print("it is not a prime number")
break
else:
print("it is prime")
checkprime(n)
Program 19
Write a program to input the salary and calculate tax to be paid according to the given slab.
a=int(input("enter your income"))
if a>=80000:
print("you have to give 10% tax",a*10/100)
elif a>=60000:
print("you have to give 8% tax",a*8/100)
elif a>=50000:
print("you have to give 7% tax",a*7/100)
else :
print("you have to give 5% tax",a*5/100)
Program 20
WAP to arrange string char. In such a way that lowercase letters come first the uppercase
then any special char. Or string
chr=["@","!","#"]
s=input("Enter string")
ns=""
for i in s:
if i.islower():
ns=ns+i
for i in s:
if i.isupper():
ns=ns+i
for i in s:
if i.isdigit():
ns=ns+i
for i in s:
if i in chr:
ns=ns+i
print(ns,end="")
Program 21
WAP to read a string and find the sum of all digits in the string.
a=input("enter a string")
l=['1','2','3','4','5','6','7','8','9','0']
sum=0
for i in a:
if i in l:
sum=sum+int(i)
else:
pass
print(sum)
Program 22
Write a menu driven program to calculate:
1. Area of circle[A=πr2]
2. Area of square [A=a*a]
Program 23
WAP to check if the entered email is valid.
Question 1-8
Output
Questions 1-10
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",passw
ord="vbps2022",database="school")
mycursor=con.cursor()
r=int(input("Enter roll number to be updated:"))
m=int(input("Enter new marks:"))
q="update student set Marks=%s where RollNo=%s;"
data=[m,r]
mycursor.execute(q,data)
con.commit()
mycursor.execute("select * from student;")
x=mycursor.fetchall()
for i in x:
print(i)
con.close()
Program 27
Write a program to insert a record in the table student using python statements. Data should
be read from user.
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",passw
ord="vbps2022",database="school")
mycursor=con.cursor()
r=int(input("Enter roll number:"))
m=int(input("Enter marks:"))
n=input("Enter Name:")
q="insert into student values(%s,%s,%s);"
data=[r,n,m]
mycursor.execute(q,data)
con.commit()
mycursor.execute("select * from student;")
x=mycursor.fetchall()
for i in x:
print(i)
con.close()
Program 28
Write a program to create a file ‘notes.txt’ containing the following content:
We are writing
data to a
text file
f=open("notes.txt", 'w')
f.write("We are Writing\n")
f.write("data to a\n")
f.write("text file")
f.close()
Program 29
Write a program to read marks of 10 students and save it in a file – marks.txt’ .
f=open("marks.txt", 'w')
for i in range(1,11):
m=input("Enter Marks:")
f.write(m)
f.write("\n")
Program 30
Write a program to count the number of words in the text file ‘notes.txt’ .
f=open("notes.txt",'r')
S=f.read()
L=S.split()
print("Number of Words is", len(L))
Program 31
Write a program that copies the content of file- ‘source.txt’ onto ‘target.txt’ by removing all
the lines starting with ‘a’.
f=open("source.txt","w")
f.write("Jayant \n")
f.write("Computer,Science.")
f.close()
f1=open("target.txt","w")
f=open("source.txt","r")
L=f.readlines()
for i in L:
if 'a' in i :
f1.write(i)
else:
continue
f.close()
f1.close()
Program 32
Write a program to calculate the number of lines and size of text file ‘text.txt’.
f=open("student.txt", 'r')
T=f.readlines()
print("Number of Lines",len(T))
Count=0
for line in T:
Count=Count+len(line)
print("Size of file is", Count, "Bytes")
f.close()
Program 33
Write a program to add these two line to text file- ‘student.txt’ :
1)welcome to school
2)you will learn more hear
f=open("student.txt", 'a')
f.write("\nYou are welcome in our school.\n")
f.write("You will learn more here")
f.close()
Program 34
Consider a file ‘student.txt’ and find out:
1) the names which start from ‘h’.
2) the number of times ‘is’ occur.
f=open("student.txt", 'r')
T=f.readlines()
for i in T:
if i[0]=='H' or i[0]=='h':
print(i)
else:
continue
f.close()
f=open("student.txt", 'r')
T=f.read()
L=T.split()
Count=0
for i in L:
if i== 'is':
Count=Count+1
else:
continue
print(Count)
f.close()
Program 35
Write a program to read a text file and display the number of vowels/ consonants/ uppercase/
lowercase characters in the file.
f=open("file.txt",'r')
r=f.read()
a=0
b=0
c=0
d=0
for i in r:
if (i.islower()):
a=a+1
elif (i.isupper()):
b= b+1
i=i.lower()
if (i in ['a','e','i','o','u']):
c=c+1
elif(i in
['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','
t','v','w','x','y','z']):
d=d+1
f.close()
print("vowels are",c)
print("consonants are",d)
print("lowercase leters are",a)
print("uppercase letters are",b)
Program 36
Write a program to create a binary file- ‘list.dat’ .
import pickle
f=open("list.dat", 'wb')
L=[10,20,50,60,70,80]
pickle.dump(L,f)
f.close()
f=open("list.dat", 'wb')
L=[10,20,50,60,70,80]
pickle.dump(L,f)
f=open("list.dat", 'rb')
L=pickle.load(f)
print(L)
f.close()
Program 37
Write a program to read roll number and name of a student and store it in a binary file. Read
the record of ‘n’ number of students. Also display the name of a given roll number.
Source Code:
D={}
import pickle
f=open("student.dat",'wb')
n=int(input("Enter Number of Students:"))
for i in range(0,n):
Rno=int(input("Enter Roll Number:"))
Name=input("Enter Name:")
D[Rno]=Name
pickle.dump(D,f)
f.close()
f=open("student.dat",'rb')
pickle.load(f)
r=int(input("Enter Roll Number to Display:"))
if r in D:
print(D[r])
else:
print("Roll number is not present in the file")
f.close()
Program 38
Write a program to create a binary file- ‘efile.dat’ which contains number, name, basic
salary, and allowance for ‘n’ number of employees. Also display total salary of each
employee.
D={}
L=[]
import pickle
f=open("efile.dat",'wb')
n=int(input("Enter Number of Employees:"))
for i in range(0,n):
Eno=int(input("Enter Employee Number:"))
Name=input("Enter Name:")
S=int(input("Enter Salary:"))
A=int(input("Enter Allowance:"))
L=[Name,S,A]
D[Eno]=L
pickle.dump(D,f)
f.close()
f=open("efile.dat",'rb')
pickle.load(f)
for i in D:
print(D[i][0], D[i][1] + D[i][2])
f.close()
Program 39
A binary file ‘customer.dat’ has the structure: [id, customer name, category]. Write a
program to read and display the details of a customer whose category is ‘g’.
import pickle
f=open("customer.dat",'rb')
while True:
try:
L=pickle.load(f)
if L[2]=="G":
print(L[0],L[1],L[2])
except EOFError:
break
f.close()
Program 40
A binary file ‘fruit.dat’ has the structure: [fruit id, fruit name, cost]. Write a program to read
each record and display the names of those fruits whose cost is more than 200.
import pickle
f=open("fruit.dat",'rb')
while True:
try:
L=pickle.load(f)
if L[2]>200:
print(L[1])
except EOFError:
break
f.close()
Program 41
Write a program to create a binary file- ‘student.dat’ which contains student name, roll
number, class, marks, city.
import pickle
f=open("student.dat",'wb')
D={}
L=[]
while True:
n=input("Enter name of the student:")
rn=int(input("Enter roll number:"))
c=input("Enter class:")
m=int(input("Enter marks:"))
ci=input("Enter city:")
L.append(rn)
L.append(c)
L.append(m)
L.append(ci)
D[n]=L
pickle.dump(L,f)
a=input("Do you want to continue? (Y/N):")
if a=='Y' or a=='y':
continue
else:
break
f.close()
Program 42
Write a program to create a csv file ‘emp.csv’ which contains employee number, name,
designation, salary and city. Ask the data from the user.
import csv
f=open("emp.csv", 'w')
L=[]
w=csv.writer(f)
H=["Eno","Name","Designation","Salary","City"]
w.writerow(H)
n=int(input("Enter Number of Employees:"))
for i in range(n):
Eno=int(input("Enter Employee ID:"))
Ename=input("Enter Employee Name:")
ED=input("Enter Employee Designation:")
ES=int(input("Enter Employee Salary:"))
EC=input("Enter Employee City:")
L=[Eno, Ename, ED, ES, EC]
w.writerow(L)
f.close()
Program 43
Create a file ‘user.csv’ which contains a username and password from different users using a
function ‘add ()’ which will add one user at a time.
import csv
f=open("user.csv",'a',newline='')
def add():
L=[]
un=input("Enter Name:")
pd=str(input("Enter password:"))
L=[un,pd]
w=csv.writer(f)
w.writerow(L)
add()
f.close()
Program 44
Write the code to read ‘user.csv’ and find the password of a given user. Use function
‘findpassword()’.
import csv
f=open("user.csv", 'r')
def findpassword():
r=csv.reader(f)
un=input("Enter Name:")
for row in r:
if row[0]==un:
print("your password is:", row[1])
break
findpassword()
f.close()
Program 45
Create a csv file ‘book.csv’. Write the user defined function- ‘create.file()’ to input to record
of book, ‘countbooks()’ to count number of books in file. The record of the book includes
book name, author and price.
Source Code:
import csv
f=open("book.csv",'w')
def createfile():
f=open("book.csv",'a')
w=csv.writer(f)
n=int(input("Number of Books:"))
for i in range(n):
L=[]
a=int(input("Book Number:"))
b=input("Book Name:")
c=input("Author Name:")
d=int(input("Price:"))
L=[a,b,c,d]
w.writerow(L)
f.close()
def countbooks():
f=open("book.csv",'r')
R=csv.reader(f)
e=0
for i in R:
e=e+1
print("Number of Books:",e)
f.close()
createfile()
countbooks()
f.close()
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",password="Jay",
database="school")
mycursor=con.cursor()
mycursor.execute("create table Student (Rno integer, Name varchar(20),
Marks integer);")
x=mycursor.fetchall()
mycursor.execute("Insert into Student Values (1, 'Abc', 75),(2, 'Def',
85),(3, 'Ghi', 92),(4, 'Jkl', 97),(5, 'Mno', 82);")
con.commit()
mycursor.execute("select name from student where Marks>90;")
x=mycursor.fetchall()
for i in x:
print(i)
con.close()
Program 48
Write a python program to change marks of a student for the given roll number.
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",password="ishik
a",database="school")
mycursor=con.cursor()
mycursor.execute("create table Student (Rno integer, Name varchar(20),
Marks integer);")
x=mycursor.fetchall()
con.commit()
mycursor.execute("Insert into Student Values (1, 'Abc', 75),(2, 'Def',
85),(3, 'Ghi', 92),(4, 'Jkl', 97),(5, 'Mno', 82);")
rn=int(input("Enter Roll Number To Be Updated:"))
m=int(input("Enter New Marks:"))
Q="Update student set marks = %s where Rno=%s;"
data = [m,rn]
mycursor.execute(Q,data)
mycursor.execute("select * from student;")
x=mycursor.fetchall()
for i in x:
print(i)
con.commit()
con.close()
Program 49
Write a python program to insert a record in the table ‘student’. data should be read from the
user.
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",password="ishik
a",database="school")
mycursor=con.cursor()
r=int(input("Enter roll number:"))
m=int(input("Enter marks:"))
n=input("Enter Name:")
q="insert into student values(%s,%s,%s);"
data=[r,n,m]
mycursor.execute(q,data)
con.commit()
mycursor.execute("select * from student;")
x=mycursor.fetchall()
for i in x:
print(i)
con.close()
Program 50
Write a python program to display all the details of students for a given roll number.
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",password="ishik
a",database="school")
mycursor=con.cursor()
r=int(input("Enter roll number:"))
q="select * from student where Rno=%s;"
data=[r]
mycursor.execute(q,data)
x=mycursor.fetchall()
for i in x:
print(i)
con.close()