[go: up one dir, main page]

0% found this document useful (0 votes)
3 views22 pages

Tiya_cs Practical File 12A

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

COMPUTER SCIENCE

Practical file

Name- Tiya
Class - XII - A
Roll number-

Q1. Write a menu driven program that has following options,


Area of circle, area of rectangle, area of square, exit. The above program will
have user defined function that takes the parameters
As well as return the value.

def circle (r):


return 3.14*r**2
def square (s):
return s*s
def rectangle(l,b):
return l*b
print(" 1. area of circle \n 2. area of square \n 3. area of rectangle \n 4. exit")
opt=int(input("enter one option"))
if opt==1:
r=int(input("enter the radius"))
print("area of circle is:", circle(r))
elif opt==2:
s=int(input("enter the length of side"))
print("area of square is:",square(s))
elif opt==3:
l=int(input("enter the length of rectangle"))
b=int(input("enter the breadth of rectangle"))
print("area of rectangle is:",rectangle(l,b))
elif opt==4:
exit()
else:
print("enter valid number and try again")

Q2. . WAP to display a Fibonacci series. The first two numbers and the length
of the Will be entered by the user.

a=int(input("enter the first number"))


b=int(input("enter the second number"))
l=int(input("enter the length of series"))
print(a,b,end='')
for i in range(l-2):
c=a+b
print(c,end='')
a,b=b,c

Q3. WAP to Input a string and determine whether it is a palindrome or not.


a=input("enter the word")
j=len(a)-1
flag=1
for i in range(len(a)//2):
if a[i]!=a[j]:
print("it's not palindrome")
flag=0
break
j-=1
else:
print(" yes it's palindrome")

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)

list_num=eval(input('enter the list:'))


remove_even(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.

create table student


(
id integer primary key,
name varchar(20)not null,
marks float,
DOB date,
gender char(2),
aadhaar_id integer not null unique
)

Q17.Frame the SQL queries for the following:

a) To create a database sidheshwar.


create database sidheshwar

b) To use the database sidheshwar.


use sidheshwar

c) To delete the database sidheshwar.


drop database sidheshwar

d) To display the list of databases.


Show databases;

e) To display the list of tables.


show tables

f) To display the structure of the table student.


desc student

g) To delete the table student.


drop table student

h) To add a new column code(type: integer) in table student.


alter table student
add code integer

i) To delete column code(type: integer) from table student


alter table student
drop column code

j) To set an existing column id as primary key in table student.


alter table student
add primary key(id)

k) To delete an existing column id as primary key from table student


alter table student
drop primary key
Q18.

A. Display the data of the entire table GAMES.


select * from games

B. To display details of those games which have PrizeMoney more than 7000.
select * from games
where prizemoney>7000

C. To display the content of the Games table in ascending order of


ScheduledDate.
select * from games
order by scheduledDate

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’

G. Display the details of the Player Jatin from Mumbai.


select * from players
name= ‘jatin’ and city = ‘Mumbai’

H. To find the average of PrizeMoney from the table Games.


select avg(prizemoney) from games

I To display the details of the player whose name starts from ‘S’.
select * from player
where name like ‘s%’

J. Find the maximum and the minimum ScheduledDate.


select min(scheduledDate) max(scheduledDate) from games

K. Increase the PrizeMoney of TableTennis by Rs. 500


update games
set prizemoney = prizemoney + 500
where Gname = ‘table tennis’

L. Display GameName and Name from tables Games and Player.

M. Display the details of the GAMES having Number as NULL.


select * from games
where number is NULL

N. Delete the record of Amir from Mumbai


delete from players
where name = ‘Amir’ and city = ‘Mumbai’
O. Insert a new record table PLAYER (5, Vansh, 108, ‘Nagpur’)
insert into player
values (5, Vansh, 108, ‘Nagpur’)

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

Q. display the Gamename of without repetition


select gamename from games
group by gamename

R. Display the details of the GAMES having Number not NULL


select * from games
where number is not null

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

You might also like