CS Class 12
CS Class 12
Code
import csv
f=open("Book.csv","a")
k=csv.writer(f)
while True:
A=input("Press N to stop")
if A=="N":
break
f.close()
Output
Press N to stopY
Press N to stopN
a)
Code
import csv
f=open("Book.csv","r")
k=csv.reader(f)
for i in k:
print(i)
f.close()
Output
b)
Code
import csv
f=open("Book.csv","r")
k=csv.reader(f)
for i in k:
if i[1] in ["Anant","Anushka"]:
print(i)
f.close()
Output
c)
Code
import csv
f=open("Book.csv","r")
k=csv.reader(f)
for i in k:
if int(i[2])>300:
print(i[1])
f.close()
Output
RD Sharma
Question 3 Write a program to take values of variant and origin from user and store them
in a csv file.
Code
import csv
f=open("Covid.csv","a")
k=csv.writer(f)
while True:
variant=input("Enter COVID variant name")
Origin=input("Enter country of origin")
k.writerows([variant,Origin])
Ans=input("Do you want to continue?,Yes/No")
if Ans=="No":
break
f.close()
Output
Question 4 Using CODING, take values from user till the time user says 'N", put these
details in csv file. Content to be asked are as follows BookCode, author, Price.
Code
import csv
f=open("Book.csv","a")
k=csv.writer(f)
while True:
BookCode=int(input("Enter the book code"))
Author=input("Enter the name of author")
Price=int(input("Enter the price"))
A=input("Press N to stop")
if A=="N":
break
f.close()
Output
Question 4 considering the previous question, do following:-
a) Display all the records on screen
b) Display only those records where author name is "Anant" or "Anushka"
c) Display author name where Price is more than Rs 300.
a)
Code
import csv
f=open("Book.csv","r")
k=csv.reader(f)
for i in k:
print(i)
f.close()
Output
['Book Code ', 'Author', 'Price']
['1', 'Ruskin Bond', '120']
['2', 'RD Sharma', '100']
['3', 'Satyajit Ray', '150']
b)
Code
import csv
f=open("Book.csv","r")
k=csv.reader(f)
for i in k:
if i[1] in ["Anant","Anushka"]:
print(i)
f.close()
Output
['4', 'Anant', '100']
c)
Code
import csv
f=open("Book.csv","r")
k=csv.reader(f)
for i in k:
if int(i[2])>300:
print(i[1])
f.close()
Output
RD Sharma
Question 5 Read a csv file with the following structure Acc no, acc name, deposit,
withdraw. The program will display total money for each customer.(deposit-withdraw)
Code
import csv
f=open("Account.csv","r")
k=csv.reader(f)
for i in k:
print(int(i[2])+int(i[3]))
f.close()
Output
14000
16000
10800
Code
import pickle
def Function():
A=open("STUDENT.DAT","ab")
S_ADmno=input("Enter your admission number")
S_Name=input("Enter your name")
Percentage=float(input("Enter your percentage"))
B=[S_ADmno,S_Name,Percentage]
pickle.dump(B,A)
A.close()
Function()
def Display():
A=open("STUDENT.DAT","rb")
while True:
try:
C=pickle.load(A)
if float(C[2])>float(75):
print(C)
except:
break
A.close()
Display()
Output
Code
import pickle
def Function():
A=open("SPEED.DAT","ab")
vehicletype=input("Enter vehicle type")
no_of_wheels=int(input("Enter no of wheels"))
B=(vehicletype,no_of_wheels)
pickle.dump(B,A)
A.close()
Function()
def showfile():
A=open("SPEED.DAT","rb")
n=0
while True:
try:
C=pickle.load(A)
n+=1
print(C)
except:
break
print("number of records present in the file are->",n)
A.close()
showfile()
Output
Question 4 Write a function in PYTHON to search for a BookNo from a binary file
“BOOK.DAT”, assuming the binary file is containing the records of the following
type:
{"BookNo":value, "Book_name":value}
Assume that BookNo is an integer.
Code
import pickle
def Function():
A=open("BOOK.DAT","ab")
B={}
BookNo=int(input("Enter book number"))
Book_name=input("Enter book name")
B[BookNo]=Book_name
pickle.dump(B,A)
A.close()
Function()
def Display():
A=open("Book.DAT","rb")
n=int(input("Enter a book number"))
while True:
try:
C=pickle.load(A)
print(C[n])
except:
break
A.close()
Display()
Output
Question 5 Write a function in PYTHON to search for a laptop from a binary file
“LAPTOP.DAT” containing the records of following type. The user should enter
the model number and the function should display the details of the laptop.
[ModelNo, RAM,] where ModelNo, RAM, HDD, Details HDD are integers, and Details is
a string.
Code
import pickle
def Function():
A=open("LAPTOP.DAT","ab")
ModelNo=int(input("Enter model number"))
RAM=int(input("Enter RAM"))
HDD=int(input("Enter HDD"))
Details HDD=int(input("Enter details HDD"))
Details=input("Enter details")
B=[ModelNO,RAM,HDD,Details HDD,Details]
pickle.dump(B,A)
A.close()
Function()
def Display():
A=open("LAPTOP.DAT","rb")
n=int(input("Enter a model number"))
while True:
try:
C=pickle.load(A)
if n in C:
print(C)
except:
break
A.close()
Display()
Output
Question 6 Write a function in PYTHON to search for the details (Number and Calls) of
those mobile phones which have more than 1000 calls from a binary file “mobile.dat”.
Assuming that this binary file contains records of the following type: (Number,calls)
Code
import pickle
def Function():
A=open("mobile.dat","ab")
Number=int(input("Enter mobile number"))
Calls=int(input("Enter number of calls"))
B=(Number,Calls)
pickle.dump(B,A)
A.close()
Function()
def Display():
A=open("mobile.dat","rb")
while True:
try:
C=pickle.load(A)
if C[1]>1000:
print(C)
except:
break
A.close()
Display()
Output
Question 3 Read a file "Hello.txt" and display sum of digits present in file.
For Example: If the file contains Hi Mr 1 I am learning Chapter 8 Unit 9 then Output of
the file will be
Sum of DIGITS -> 18
Code
A=open("HELLO.txt","r")
B=A.read()
C=0
for i in B:
if i.isdigit():
C+=int(i)
print("Sum of the digits=",C)
A.close()
Output
Question 4 Read a file Hello.txt, and display all the lines starting with letter 'a'.
Code
A=open("Hello .txt","r")
for i in A.readlines():
if i.startswith("a")==True:
print(i)
A.close()
OR
A=open("Hello .txt","r")
for i in A.readlines():
if i[0]=="a":
print(i)
A.close()
Output
Question 5 Read a file “hello.txt” and display total no of lines present in file.
Code
f=open("hello!.txt","r")
A=0
for i in f.readlines():
A+=1
print(A)
f.close()
OR
with open("hello.txt","r") as f:
print("lines are",len(f.readlines()))
Output
Question 6 Read a file “hello.txt” and display total no of words present in file.
Code
with open("HELLO.txt","r") as f:
print(len(f.read().split()))
Output
Question 7 Read a file”Hello.txt”, and copy all words of having length more than 4 to
another file “Sample.txt”.
Code
A=open("HELLO.txt","r")
B=open("Sample.txt","w")
for i in A.read().split():
if len(i)>4:
B.write(i)
A.close()
B.close()
Output
ABCDE
Question 8 Read a file "Hello.txt" and copy all the lowercase characters to lower.txt and
Uppercase characters to another file upper,txt.
Code
A=open("HELLO.txt","r")
B=open("lower.txt","w")
C=open("upper.txt","w")
for i in A.read():
if i.isupper():
C.write(i)
elif i.islower():
B.write(i)
A.close()
B.close()
C.close()
Output
“upper”
ABCD
”lower”
fg
SQL
Question 1 Write the SQL commands to answer the queries-based om school table.
Code
Statements