Professional Documents
Culture Documents
Science
Report File
By : Dhruv Jain
XII-B
Board Roll No:
1
Index
Python Programs
S. No. Program Page Signature
No. and Remarks
1. Write a function that receives two string arguments and 5
checks whether they are of same length (returns True else
False).
2. WAP that creates a list of all integers less than 100 that are 6
multiples of 3 & 5.
3. WAP that calculates and prints the number of seconds in a 7
year.
4. Read a text file line by line and display each word separated 8
by a #.
5. Read a text file and display the number of vowels/ 9
consonants/uppercase/lowercase characters in the file.
6. Remove all the lines that contain the character 'a' in a file 10
and write it to another file.
7. Create a binary file with name and roll number. Search for a 11
given roll number and display the name, if not found display
appropriate message.
8. Create a binary file with roll number, name, and marks. Input 12
a roll number and update the marks.
9. Write a random number generator that generates random 14
numbers between 1 and 6 (simulates a dice).
10. Create a CSV file by entering user-id and password, read and 15
search the password for given user-id.
11. Write a program to know the cursor position and print the 17
text according to the below given specifications:
o Print the initial position
o Move the cursor to 4th position
o Display next 5 characters
o Move the cursor to the next 10 characters
o Print the current cursor position
o Print next 10 characters from the current cursor position
2
12. Write a program to store customer data into a binary file 18
cust.dat using a dictionary and print them on screen after
reading them. The customer data contains ID as key, and
name, city as values.
13. Write a program to create a binary file sales.dat and write a 19
menu driven program to do the following:
a. Insert record
b. Search Record
c. Update Record
d. Display record
e. Exit
14. Read a CSV file top5.csv and print the contents in a proper 23
format. The data for top5.csv file are as following:
MySQL Operations
S. No. Operations Page Signature
No. and Remarks
1. Set-1 27
2. Set-2 29
3. Set-3 31
4. Set-4 33
5. Set-5 35
3
Python-MySQL Connectivity
S. No. Python-MySQL Interface Programs Page Signature
No. and Remarks
1. Write a MySQL connectivity program in Python to 37
o Create a database school
o Create a table students with the specifications – ROLLNO
integer, STNAME character(10) in MySQL and perform the
following operations:
▪ Insert two records in it
▪ Display the contents of the table
2. Write a menu-driven program to store data into a MySQL 38
database named shop and table customer as following:
1. Add customer details
2. Update customer details
3. Delete customer details
4. Display all customer details
3. Modify the above program and display the customer details 43
based on the following menu:
1. Display customer details by city
2. Display customer details by bill amount
3. Display customer details by name
4. Display customer details by category
4
Python Programs
Question 1: Write a function that receives two string arguments and
checks whether they are of same length(returns True else False).
def len_check(str1,str2):
if len(str1)==len(str2):
return True
else:
return False
str1=input("Enter the first string:")
str2=input("Enter the second string:")
print(len_check(str1,str2))
5
Question 2: WAP that creates a list of all integers less than 100 that are
multiples of 3 & 5.
list1=[]
for i in range(1,101):
if i%3==0 and i%5==0:
list1+=[i]
print(list1)
6
Question 3: WAP that calculates and prints the number of seconds in a
year.
7
Question 4: Read a text file line by line and display each word separated
by a #.
f=open("text.txt","r")
print("text.txt:",f.read(),sep="\n")
f=open("text.txt","r")
for line in f:
for word in line.strip():
print(word,end="#")
print()
8
Question 5: Read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file.
f=open("text.txt","r")
print("text.txt:",f.read(),sep="\n")
f=open("text.txt","r")
vowel,cons,upper,lower=0,0,0,0
for i in f.read():
if i.isalpha():
if i in ['a','e','i','o','u','A','E','I','O','U']:
vowel+=1
else:
cons+=1
if i.isupper():
upper+=1
else:
lower+=1 print("Vowels:",vowel,"\nConsonants:",cons,"\nUppercase:",upper,"\
nLowercase:",lower)
9
Question 6: Remove all the lines that contain the character 'a' in a file
and write it to another file.
f=open('text.txt','r')
print("text.txt before:",f.read(),sep="\n")
f=open("text.txt","r")
list1=[]
list2=[]
for line in f.readlines():
if 'a' in line:
list1+=[line]
else:
list2+=[line]
f=open('text.txt','w')
f.writelines(list2)
g=open('atext.txt','w')
g.writelines(list1)
g=open("atext.txt","r")
print("atext.txt:",g.read(),sep="\n")
f=open("text.txt","r")
print("text.txt after:",f.read(),sep="\n")
10
Question 7: Create a binary file with name and roll number. Search for a
given roll number and display the name, if not found display appropriate
message.
import pickle
f=open('btext.dat','wb')
n=int(input("Enter the number of students:"))
dict1={}
for i in range(n):
roll=int(input("Enter the roll number:"))
name=input("Enter the name:")
dict1[roll]=name
pickle.dump(dict1,f)
f=open('btext.dat','rb')
data=pickle.load(f)
print("btext.dat:",data,sep="\n")
num=int(input("Enter the roll number to be searched:"))
if num in data:
print("Name associated with roll number",num,"is",data[num])
else:
print("Roll Number not found!")
11
Question 8: Create a binary file with roll number, name, and marks. Input
a roll number and update the marks.
import pickle
f=open('btext.dat','wb')
n=int(input("Enter the number of students:"))
dict1={}
for i in range(n):
roll=int(input("Enter the roll number:"))
name=input("Enter the name:")
marks=int(input("Enter the marks:"))
dict1[roll]=[name,marks]
pickle.dump(dict1,f)
f=open('btext.dat','rb')
data=pickle.load(f)
print("btext.dat before:",data,sep="\n")
num=int(input("Enter the roll number of the student whose marks are to be updated:"))
if num in data:
new_marks=int(input("Enter the new marks:"))
data[num][1]=new_marks
f=open('btext.dat','wb')
pickle.dump(data,f)
f=open('btext.dat','rb')
print("btext.dat after:",pickle.load(f),sep="\n")
else:
print("Roll Number not found!")
12
13
Question 9: Write a random number generator that generates random
numbers between 1 and 6 (simulates a dice).
import random
dice=random.randint(1,6)
print("Random Number Generated:",dice)
14
Question 10: Create a CSV file by entering user-id and password, read
and search the password for given user-id.
import csv
f=open("passwords.csv","w",newline="")
g=csv.writer(f)
n=int(input("Enter the number of IDs:"))
for i in range(n):
user=input("Enter the user-id:")
password=input("Enter the password:")
g.writerow([user,password])
f.close()
f=open("passwords.csv","r")
h=csv.reader(f)
print("passwords.csv:")
for j in h:
print(j)
f.close()
f=open("passwords.csv","r")
h=csv.reader(f)
id=input("Enter the user-id to be searched:")
found=False
for k in h:
if k[0]==id:
print("Password associated with user-id",id,"is",k[1])
found=True
if found==False:
print("User-id not found!")
15
16
Question 11: Write a program to know the cursor position and print the
text according to the below given specifications:
o Print the initial position
o Move the cursor to 4th position
o Display next 5 characters
o Move the cursor to the next 10 characters
o Print the current cursor position
o Print next 10 characters from the current cursor position
f=open("para.txt")
print("para.txt:")
print(f.read())
f.seek(0)
print("Initial Position:",f.tell())
f.seek(4)
print("Next 5 characters:",f.read(5))
f.seek(19)
print("Current Position:",f.tell())
print("Next 10 characters:",f.read(10))
17
Question 12: Write a program to store customer data into a binary file
cust.dat using a dictionary and print them on screen after reading them.
The customer data contains ID as key, and name, city as values.
import pickle
f=open("cust.dat","wb")
n=int(input("Enter the number of entries:"))
for i in range(n):
d={}
id=input("Enter the customer ID:")
name=input("Enter the customer name:")
city=input("Enter the city:")
d[id]=[name,city]
pickle.dump(d,f)
f=open("cust.dat","rb")
try:
print("Customer Data")
while True:
print(pickle.load(f))
except:
pass
18
Question 13: Write a program to create a binary file sales.dat and write a
menu driven program to do the following:
a. Insert record
b. Search Record
c. Update Record
d. Display record
e. Exit
import pickle
while True:
print("~~~MENU~~~")
print("1. Insert Record")
print("2. Search Record")
print("3. Update Record")
print("4. Display Record")
print("5. Exit")
c=int(input("Enter the choice:"))
if c==1:
f=open("sales.dat","ab")
id=input("Enter the product ID:")
name=input("Enter the product name:")
price=int(input("Enter the product price:"))
qty=int(input("Enter the product quantity:"))
pickle.dump([id,name,price,qty],f)
elif c==2:
f=open("sales.dat","rb")
id=input("Enter the id of the product to be searched:")
try:
19
while True:
r=pickle.load(f)
if r[0]==id:
print(["ID", "Name", "Price", "Quantity"])
print(r)
break
except:
print("Record Not Found!")
elif c==3:
f = open("sales.dat", "rb")
id = input("Enter the id of the product to be updated:")
l=[]
try:
while True:
r=pickle.load(f)
if r[0]==id:
name = input("Enter the new product name:")
price = int(input("Enter the new product price:"))
qty = int(input("Enter the new product quantity:"))
r=[id,name,price,qty]
l+=[r]
except:
pass
f=open("sales.dat","wb")
for i in l:
pickle.dump(i,f)
elif c==4:
f=open("sales.dat","rb")
print(["ID","Name","Price","Quantity"])
try:
20
while True:
print(pickle.load(f))
except:
pass
elif c==5:
exit()
else:
print("Enter a valid choice!")
21
22
Question 14: Read a CSV file top5.csv and print the contents in a proper
format. The data for top5.csv file are as following:
import csv
f=open("top5.csv","r")
g=csv.reader(f)
for i in g:
print(i)
23
Question 15: Write a menu-driven python program to implement all
stack operations.
def isEmpty(stack):
if stack==[]:
return True
else:
return False
def Push(stack,item):
stack.append(item)
def Pop(stack):
if isEmpty(stack):
print("Stack Underflow!")
else:
return stack.pop()
def Peek(stack):
if isEmpty(stack):
print("Stack Empty!")
else:
print(stack[-1])
def Display(stack):
if isEmpty(stack):
print("Stack Empty!")
else:
print("Stack:")
for i in range(len(stack)-1,-1,-1):
print(stack[i])
stack=[]
while True:
24
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display")
print("5. Exit")
c=int(input("Enter the choice:"))
if c==1:
item=input("Enter the item to push:")
Push(stack,item)
elif c==2:
print("Popped Item:",Pop(stack))
elif c==3:
Peek(stack)
elif c==4:
Display(stack)
elif c==5:
exit()
else:
print("Enter a valid choice!")
25
26
MySQL Operations
Set-1: Consider the following MOVIE table and write the SQL queries
based on it.
27
b) Select DISTINCT Type from MOVIE;
28
Set-2: Consider the given table patient and write following queries:
29
3. Select count(DISTINCT department) from patient;
30
Set-3: Suppose your school management has decided to conduct cricket
matches between students of Class XI and Class XII. Students of each
class are asked to join any one of the four teams with team names –
Tehlka, Toofan, Aandhi and Shailab. During summer vacations, various
matches will be conducted between these teams. Help your sports
teacher to do the following:
a. Create a database “Sports”.
b. Create a table “TEAM” with following considerations:
o It should have a column TeamID for storing an integer value
between 1 to 9, which refers to unique identification of a team.
o Each TeamID should have its associated name (TeamName),
which should be a string of length less than 10 characters. (4 team
names given above)
o Using table level constraint, make TeamID as the primary key.
c. Show the structure of the table TEAM using a SQL statement.
d. As per the preferences of the students four teams were formed as
given below. Insert these four rows in TEAM table:
o Row 1: (1, Tehlka)
o Row 2: (2, Toofan)
o Row 3: (3, Aandhi)
o Row 3: (4, Shailab)
o Show the contents of the table TEAM using a DML statement.
31
b) Create table TEAM(TeamID int check(TeamID between 1 and 9),TeamName
varchar(10) check(length(TeamName)<10), primary key(TeamID));
c) Desc TEAM;
32
Set-4: Now create another table MATCH_DETAILS and insert data as
shown below. Choose appropriate data types and constraints for each
attribute.
33
select MatchID, FirstTeamID,SecondTeamID,FirstTeamScore,SecondTeamScore, TeamName from
MATCH_DETAILS M, TEAM T where (T.TeamID in (FirstTeamID,SecondTeamID)) AND (MatchID='M1') AND
(FirstTeamScore>70 AND SecondTeamScore>70);
34
Set-5: Consider the stock table to answer the below queries.
35
b) select dcode,max(unitprice) from stock group by dcode;
36
Python-MySQL Interface Programs
Question 1: Write a MySQL connectivity program in Python to
o Create a database school
o Create a table students with the specifications – ROLLNO integer,
STNAME character(10) in MySQL and perform the following operations:
▪ Insert two records in it
▪ Display the contents of the table
37
Question 2: Write a menu-driven program to store data into a MySQL
database named shop and table customer as following:
1. Add customer details
2. Update customer details
3. Delete customer details
4. Display all customer details
38
cur.execute("insert into customer values({},'{}','{}','{}',{})".format(cid,cname,city,cat,amt))
con.commit()
print("Customer Details Added!")
elif c==2:
cid=int(input("Enter the customer ID to update the details of:"))
cur.execute("select * from customer where CID={}".format(cid))
print("Current Details")
print(("CID","CName","City","Category","Bill_Amount"))
print(cur.fetchone())
39
else:
print("Enter a valid choice!")
40
41
42
Question 3: Modify the above program and display the customer details
based on the following menu:
1. Display customer details by city
2. Display customer details by bill amount
3. Display customer details by name
4. Display customer details by category
43
print("Details by City")
print(("CID", "CName", "City", "Category", "Bill_Amount"))
for i in r:
print(i)
elif c==2:
amt=float(input("Enter the bill amount to display the customer details of:"))
cur.execute("select * from customer where Bill_Amount={}".format(amt))
r = cur.fetchall()
if len(r) == 0:
print("No Records!")
continue
print("Details by Bill Amount")
print(("CID", "CName", "City", "Category", "Bill_Amount"))
for i in r:
print(i)
elif c==3:
cname=input("Enter the name to display the customer details of:")
cur.execute("select * from customer where CName='{}'".format(cname))
r = cur.fetchall()
if len(r) == 0:
print("No Records!")
continue
print("Details by Name")
print(("CID", "CName", "City", "Category", "Bill_Amount"))
for i in r:
print(i)
elif c==4:
cat=input("Enter the category to display the customer details of:")
cur.execute("select * from customer where Category='{}'".format(cat))
r = cur.fetchall()
44
if len(r) == 0:
print("No Records!")
continue
print("Details by Category")
print(("CID", "CName", "City", "Category", "Bill_Amount"))
for i in r:
print(i)
elif c==5:
exit()
else:
print("Enter a valid choice!")
45
46