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

CS Class 12

The document discusses various questions related to file handling in Python. It covers topics like: 1. Reading and writing data to CSV files from user input and performing operations like filtering and displaying data. 2. Reading and writing objects to binary files using pickle module and performing search operations. 3. Reading text files and extracting/displaying information like lowercase characters, word counts, line counts etc. 4. Copying/filtering data between files based on certain criteria. The questions provide code snippets to demonstrate various file handling tasks in Python along with sample input/output.

Uploaded by

Reyan Bisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

CS Class 12

The document discusses various questions related to file handling in Python. It covers topics like: 1. Reading and writing data to CSV files from user input and performing operations like filtering and displaying data. 2. Reading and writing objects to binary files using pickle module and performing search operations. 3. Reading text files and extracting/displaying information like lowercase characters, word counts, line counts etc. 4. Copying/filtering data between files based on certain criteria. The questions provide code snippets to demonstrate various file handling tasks in Python along with sample input/output.

Uploaded by

Reyan Bisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PYTHON SQL CONNECTIVITY

CSV FILE HANDLING


Q1)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

Enter the book code123

Enter the name of authorRD Sharma

Enter the price1200

Press N to stopY

Enter the book code1234

Enter the name of authorRuskin Bond

Enter the price120

Press N to stopN

Question 2 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 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

BINARY FILE HANDLING


Question 1 Write a function in Python to search and display details of all those students
using, display(L) function whose stream is “HUMANITIES” from pickled file
“Student.dat”. Assuming the pickled file is containing the following fields :
RNO,Name,Stream,Percentage
Code
import pickle
def Function():
A=open("Student.dat","ab")
RNO=int(input("Enter your roll no."))
Name=input("Enter your name")
Stream=input("Enter your stream")
Percentage=int(input("Enter your percentage"))
B=[RNO,Name,Stream,Percentage]
pickle.dump(B,A)
A.close()
Function()
def display():
A=open("Student.dat","rb")
while True:
try:
C=pickle.load(A)
if C[2]=="HUMANITIES":
print(C)
except:
break
A.close()
display()
Output

Question 2 Given a binary file “STUDENT.DAT”, containing records of the following


type:

[S_Admno, S_Name, Percentage]


Where these three values are:
S_Admno – Admission Number of student (string)
S_Name – Name of student (string)
Percentage – Marks percentage of student (float)
Write a function in PYTHON that would read contents of the file “STUDENT.DAT”
and display the details of those students whose percentage is above 75.

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

Question 3 Assuming the tuple Vehicle as follows:


( vehicletype, no_of_wheels)
Where vehicletype is a string and no_of_wheels is an integer.
Write a function showfile() to read all the records present in an already existing
binary file SPEED.DAT and display them on the screen, also count the number of
records present in the file.

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

TEXT FILE HANDLING


Question 1 Read a file and display all lowercase characters from the file. "HELLO.txt"
Code
a=open("HELLO.txt","r")
b=a.read()
for i in b:
if i.islower():
print(i)
a.close()
Output
Question 2 Read a file "Hello.txt" and display total no of lowercase and uppercase
characters from the file.
Code
A=open("HELLO.txt","r")
B=A.read()
C,D=0,0
for i in B:
if i.islower():
C+=1
elif i.isupper():
D+=1
print("Total number of lowercase characters->",C)
print("Total number of uppercase characters->",D)
A.close()
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.

Schoolid Sname Type Number


St01 Shiv Male 50
St02 Shanu Female 30
St03 Tinku Male 45
St04 Joy Male 15
St05 Aizel Female 47

A) To insert the following record (“St06”,”Kirti”,”Femal”,50)


B) To display only those students whose number is more than 40.
C) To display those records whose type is “Male”.
D) To modify the records by increasing number by 10.
E) To delete the record of student St03 from the table

Code
Statements

A) insert into School value("St06","Kirti","Female",50);

B) select* from school where Number>40;

C) select* from school where Type="Male";

D) update school set Number=Number+10;

E) delete from school where Schoolid="St03";

You might also like