Tiya_cs Practical File 12A
Tiya_cs Practical File 12A
Tiya_cs Practical File 12A
Practical file
Name- Tiya
Class - XII - A
Roll number-
Q2. . WAP to display a Fibonacci series. The first two numbers and the length
of the Will be entered by the user.
Q4. WAP to input a list of numbers and swap elements at even locations with
the elements at the odd location.
l=[10,20,30,40,50,60]
j=1
for i in range(0,len(l),2):
l[i],l[j]=l[j],l[i]
j+=2
print(l)
Q5 WAP to find and display the sum of all the integers ending with 2 in a list.
l=[23,22,45,12,25]
sum=0
for i in l:
if i%10==2:
sum+=i
print("the sum is", sum)
Q6. WAP to create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have marks above.
(85)
ans='yY'
d={}
while ans in 'yY':
roll_no=int(input("Enter roll no: "))
name=input("Enter name: ")
marks=int(input("Enter marks: "))
d[roll_no]=[name,marks]
ans=input("do you want to add more?")
for i in d:
if(d[i][1]>75):
print(d[i][0])
Q7. WAP to check if the number entered by the user is Prime or not. Pass N as
Argument to the function.
def prime(n):
flag=0
for i in range(2,n):
if n%i==0:
print('it is not a prime number')
flag=1
break
if flag==0:
print('it is a prime number')
n=int(input('enter the number'))
prime(n)
Q8. WAP with function remove_even(list_num) that takes the list with numbers
as parameter and returns the updated list after removing the even values in the
list.
def remove_even(list_num):
for i in list_num:
if(i%2==0):
list_num.remove(i)
print('list after removing even values:',list_num)
Q9. Read a file abc.txt and display the number of characters/ vowels/
consonants/ uppercase/lowercase/ digits present in the file.
def read_data():
f=open('abc.txt','r')
data=f.read()
f.close()
c=v=char=u=l=d=0
for i in range(len(data)):
if data[i].islower():
l+=1
elif data[i].isupper():
u+=1
elif data[i].isdigit():
d+=1
elif data[i].isalpha():
char+=1
elif data[i] not in "AEIOUaeiou":
c+=1
elif data[i] in "AEIOUaeiou":
v+=1
print('lower',l,'upper',u,'vowel',v,'consonant',c,'character',char,'digit',d,end='\
n')
read_data()
Q10. WAP to read a text file abc.txt and copy all the words that start with the
Character ‘T’ and write it to another file bcd.txt.
def write_data():
f=open('abc.txt','w')
data="a tiny rabbit took a trip to the garden"
f.write(data)
f.close()
def copy_data():
f1=open('abc.txt','r')
f2=open('bcd.txt','w')
data=f1.read()
words=data.split()
for i in words:
if i[0] in 'tT':
f2.write(i)
print('content copied')
f1.close()
f2.close()
write_data()
copy_data()
Q11. WAP to read a file abc.txt and count and display the occurrence of the
word “my” in the file.
def count_my():
f=open('abc.txt','r')
data=f.read()
list=data.split()
c=0
for i in list:
if i.lower()=="my":
c+=1
print('number of times my are',c)
f.close()
count_my()
Q12. WAP to create a binary file with name and roll number. Search for a given
roll number and display the name, if not found display an appropriate
message.
import pickle
def write_data():
f=open('a.dat','wb')
ans='yY'
while ans in 'Yy':
r=int(input("enter roll number:"))
n=input("enter name:")
l=[r,n]
pickle.dump(l,f)
print("data added")
ans=input("do you want to add more records (y/n):")
f.close()
def search_data():
f=open('a.dat','rb')
print()
s=int(input("enter the roll number you want to search:"))
try:
while True:
data=pickle.load(f)
if data[0]==s:
print('name is',data[1])
except:
f.close()
write_data()
search_data()
Q13. WAP to create a binary file with roll number, name and marks. Input a roll
number and update the marks.
import pickle
def write_data():
f=open('a.dat','wb')
while True:
r=int(input('enter roll number'))
n=input('enter name')
m=float(input('enter marks'))
data=[r,n,m]
pickle.dump(data,f)
ans=input('do you want to add more?')
if ans.upper()=='N':
break
f.close()
def update_data():
f=open('a.dat','rb')
r=int(input('enter roll number'))
m=float(input('enter the new marks'))
flag=0
try:
while True:
pos=f.tell()
data=pickle.load(f)
if data[0]==r:
flag=1
data[2]=m
f.seek(pos)
pickle.dump(data,f)
break
except:
if flag==0:
print('roll number not found')
f.close()
write_data()
update_data()
Q14. WAP to write, count and display the no. of records present in abc.csv file.
import csv
def write_data():
f=open('abc.csv','w',newline='')
rows=[['id','name', 'marks'],[1,'giya',45],[2,'riya',67],[3,'tiya',89]]
w=csv.writer(f)
w.writerows(rows)
f.close()
def count_data():
f=open('abc.csv','r')
data=csv.reader(f)
c=0
for i in data:
c+=1
print("total records are:",c-1)
f.close()
write_data()
count_data()
15. Write a random number generator that generates random numbers between
1 and 6 (simulates a dice).
import random
print("DICE ROLL GAME")
ans='yY'
while ans=='yY':
dice=random.randint(1,6)
ans=input("do you want to roll?,(y/n)")
print(dice)
Q16.
B. To display details of those games which have PrizeMoney more than 7000.
select * from games
where prizemoney>7000
D. To count and display the number of GameName from the Games table.
select count(GameName) from games
E. To display the details having 102<=GCode<=105 from table PLAYER.
select * from games
where Gcode>=102 and Gcode<105
F To display the details of the Players from city delhi, Mumbai, Agra.
select * from players
where city = ‘Delhi’ and city= ‘Mumbai’ and city = ‘Agra’
I To display the details of the player whose name starts from ‘S’.
select * from player
where name like ‘s%’
P. Calculate and Display the tax on PrizeMoney. The tax will be calculated as
10% of the PrizeMoney. The heading of the output column should be ‘Tax’
select prizemoney prizemoney*10/100 as tax from games
Q19. WAP to enter data according to the user in a table student of database
school and also display the data.
import mysql.connector as a
mydb=a.connect(host='localhost',user='root',password='6915',database='scho
ol')
mycursor=mydb.cursor()
def insert_display():
id=int(input('enter id:'))
n=input('enter name:')
m=float(input('enter marks:'))
query='insert into student(id,name,marks) values({},"{}",{})'.format(id,n,m)
mycursor.execute(query)
mydb.commit()
mycursor.execute('select * from student')
for i in mycursor:
print(i)
insert_display()
Q20 WAP to update data according to the user in a table student of database
school and also display the data.
import mysql.connector as a
mydb=a.connect(host='localhost',user='root',password='6915',database='scho
ol')
mycursor=mydb.cursor()
def update_display():
id=int(input('enter id of student whose marks are to be updated:'))
m=float(input('enter new marks'))
query='update student set marks={} where id={}'.format(m,id)
mycursor.execute(query)
mydb.commit()
mycursor.execute('select * from student')
for i in mycursor:
print(i)
update_display()
Q21 WAP to delete data according to the user in a table student of database
school and also display the data.
import mysql.connector as a
mydb=a.connect(host='localhost',user='root',password='6915',database='scho
ol')
mycursor=mydb.cursor()
def delete_display():
id=int(input('enter id of student whose details are to be deleted:'))
query='delete from student where id={}'.format(id)
mycursor.execute(query)
mydb.commit()
mycursor.execute('select * from student')
for i in mycursor:
print(i)
delete_display()