Computer science practial class12
Computer science practial class12
f=open(r'E:\yash codes\MARKS.DAT','wb')
n=int(input('Enter number of students ='))
for i in range(n):
name=input('Enter student name =')
eng=int(input('Enter number of student in English ='))
hindi=int(input('Enter number of student in Hindi ='))
maths=int(input('Enter number of student in Maths ='))
data={ 'Name' = name , 'English' = eng , 'Hindi' = hindi , 'Maths' = maths }
pickle.dump(data,f)
f.close()
f=open(r'E:\yash codes\MARKS.DAT','rb')
try:
while True:
a=pickle.load(f)
m=(a['English']+a['Hindi']+a['Maths'])/3
if m>=90:
print('Name of topper is',a['Name'])
except:
f.close()
#Q2
def createcsv():
with open(r'.\STUDENT.CSV','w',newline='') as f:
w=csv.writer(f)
w.writerow(['ROLLNO','NAME','CLASS'])
n=int(input('Enter number of students ='))
for i in range(n):
r=int(input('Eter roll number student ='))
name=input('Enter name of student =')
c=int(input('Enter class of student ='))
w.writerow([r,name,c])
print()
def readcsv():
with open(r'.\STUDENT.CSV','r') as f:
r=csv.reader(f)
for i in r:
print(i)
#Q3
with open(r'.\SAMPLE.TXT' ,'w') as f:
f.writelines(["Desire, when it stems from the heart and spirit, when it is pure
and intense, possesses awesome electromagnetic energy.\n",
"This energy is released into the ether each night, as the mind falls into the
sleep state. Each morning it returns to the \n"
,"conscious state reinforced with the cosmic currents. That which has been
imaged will surely and certainly be manifested.\n"
,"You can rely, young man, upon this ageless promise as surely as you can rely
upon the eternally unbroken promise of \n"
,"sunrise... and of Spring.\n"])
with open(r'.\SAMPLE.TXT') as f:
a=f.read()
b=a.split()
for i in b:
if ('a' in i) or ('A' in i):
print(i)
#Q4
def form_file():
with open(r"E:\yash codes\practicals\MARKS.DAT",'wb') as f:
n=int(input('Enter number of stidents ='))
for i in range(n):
r=int(input('Enter rollno of student ='))
name=input('Enter name of student =')
m=float(input('Enter total marks ='))
data={'Rollno':r,'Name':name,'Total_marks':m}
pickle.dump(data,f)
def update_file():
with open(r"E:\yash codes\practicals\MARKS.DAT",'ab+') as f:
f.seek(0)
r=int(input('Enter rollno of student whose marks should be updated='))
m=float(input('Enter new total marks ='))
while True:
a=f.tell()
d=pickle.load(f)
if d['Rollno']==r:
f.seek(a)
d['Total_marks']=m
pickle.dump(d,f)
break
def display_file():
with open("E:\yash codes\practicals\MARKS.DAT",'rb') as f:
while True:
a=pickle.load(f)
print(a)
#Q5
f=open('USER.TXT','w')
f.writelines(["Desire, when it stems from the heart and spirit, when it is pure and
intense, possesses awesome electromagnetic energy.\n",
"This energy is released into the ether each night, as the mind falls into the
sleep state. Each morning it returns to the \n"
,"conscious state reinforced with the cosmic currents. That which has been
imaged will surely and certainly be manifested.\n"
,"You can rely, young man, upon this ageless promise as surely as you can rely
upon the eternally unbroken promise of \n"
,"sunrise... and of Spring.\n"])
f.close()
f=open('USER.TXT','r')
f.seek(0)
a=input('Enter a word =')
b=f.readlines()
x=[]
for i in b:
if a in i:
c=i.count(a)
for j in range(c):
i=i.replace(a,'')
x.append(i)
f.close()
f=open('USER.TXT','w')
f.writelines(x)
f.close()
f=open('USER.TXT','r')
print(f.read())
f.close()
#Q6
x=[]
while True:
a=random.randint(1,10)
if len(x)==10:
break
elif a not in x:
x.append(a)
print('Ten random numbers between 1 to 10 =',x)
#Q7
a=eval(input('Enter a list of numbers ='))
b=a.copy()
c=max(b)
b.remove(c)
print(f'Second largest number in list{a} is {max(b)}')
#Q8
a=int(input('Enter the number ='))
r=0
while a>0:
rem=a%10
r=r*10+rem
a//=10
print('reversed nunmber =',r)
#Q9
a=input('Write the string =')
b=list(a)
for i in range(1,len(a),2):
b[i-1],b[i]=b[i],b[i-1]
for i in b:
print(i,end='')
print()
#Q10
def EVEODD(l1,l2):
l=l1+l2
l1.clear()
l2.clear()
for i in l:
if i%2==0:
l1.append(i)
else:
l2.append(i)
return l1,l2
#Q11
n=int(input('Enter number of rows in pattern ='))
for i in range(1,n+1):
a=0
for j in range(1,i+1):
a=a+j
if j==i:
print(j,'=',a,sep='')
else:
print(j,'+',end='',sep='')
#Q12
n=int(input('Enter a number ='))
b=n
for i in range(2,n):
if n%i==0:
print(n,'is NOT a PRIME NUMBER')
break
else:
r=0
while n>0:
a=n%10
r=r*10+a
n=n//10
for i in range(2,r):
if r%i==0:
print(b,'is NOT a TWISTED PRIME')
break
else:
print(b,'is a TWISTED PRIME')
#Q13
def find3(l):
s=0
for i in l:
if (str(i))[-1]=='3':
s+=i
return s
#Q14
def dic(d1,d2):
d={}
for i in d1:
if i in d2:
d.update({i:(d1[i],d2[i])})
return d
#Q15
def symmetric_str(a):
if a==a[::-1]:
print(a,'string is symmertical')
else:
print(a,'string is not symmertical')
#Q16
stack=[]
def push():
r=int(input('Enter a student roll number ='))
name=input('Enter student name =')
m=int(input('Enter Total marks ='))
data={'Rollno':r,'Name':name,'Marks':m}
stack.append(data)
def pop():
if not(stack == []):
print(stack.pop())
else:
print('Stack empty')
def display():
a=stack[::-1]
for i in a:
print(i)
print('Stack empty')
#Q17
n=int(input('Enter size of list ='))
stack=[]
def push():
if len(stack)==n:
print('stack overflow')
else:
a=int(input('Entre number ='))
stack.append(a)
def pop():
if len(stack)==n:
print('stack underflow')
else:
print(stack.pop())
def display():
a=stack[::-1]
for i in a:
print(i)
#Q18
d={}
n=int(input('Enter number of students ='))
for i in range(n):
name=input('Enter name of student =')
marks=int(input("Enter student's marks ="))
d.update({name:marks})
stack=[]
def push():
for i in d:
if d[i]>=75:
stack.append(i)
def pop():
for i in stack:
print(stack.pop(),end=' ')
#Q19
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',passwd='root')
cur=con.cursor()
database=input('Enter name of Database =')
cur.execute(f'use {database}')
cur.execute('create table employee(empno int primary key,name varchar(20),gender
varchar(10),sal int,desig varchar(20))')
n=int(input('Enter number of enmployees ='))
for i in range(n):
empno=int(input('Enter Employee number ='))
name=input('Enter Employee name =')
g=input('Enter Employee gender =')
sal=int(input('Enter Employee salary ='))
d=input('Enter Employee designation =')
cur.execute(f'insert into employee values ({empno},{name},{g},{sal},{d})')
con.commit()
con.close()
#Q20
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',passwd='root')
cur=con.cursor()
database=input('Enter name of Database =')
cur.execute(f'use {database}')
desig=input('Enter Employee designation =')
cur.execute(f'select * from employee where designation={desig} order by salary')
data = cur.fetchall()
for i in data:
print(i)
con.close()
#21
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',passwd='root')
cur=con.cursor()
database=input('Enter name of Database =')
cur.execute(f'use {database}')
empno=int(input('Enter employee number ='))
cur.execute(f'select * from employee where empno={empno}')
data=cur.fetchall()
cur.execute(f'delete from employee where empno={empno}')
con.commit()
con.close()
#Q22
#1
select name , d.dcode , location from employee e , dept d where d.dcode = e.dcode ;
#2
select location , count(eno) 'number of employee' from employee e , dept d where
d.dcode = e.dcode order by location ;
#3
select max(doj) , max(dob) from employee order by gender ;
#4
select * from employee where dob like '2009%' ;
#5
select * from employee where dcode = (select dcode from dept where
department='finance') ;
#Q23
#1
select cname , address from customer where cno = (select cno from transaction where
amount>10000) ;
#2
select * from transaction where dot like '%09%';
#3
select distinct address from customer;
#4
select trno , amount , (amount*0.05) as 'tax' from transaction;
#5
select cname from customer where cno not in (select cno from transaction) ;
#6
#1
select ename from emp where job = 'clerk' and sal between 1000 to 2000 ;
#2
select job , count(job) 'number of employee' from emp group by job ;
#3
select empno , ename , sal from emp where job = 'manager' and deptno = 30 ;
#4
select count(job) 'number of job' from emp ;
#5
select deptno , sum(sal) 'total salary' from emp group by deptno;
#7
#1
select deptno from emp group by deptno having count(*) > 3 ;
#2
select ename from emp where ename like '%A%' ;
#3
select * from emp where hiredate like '%81' ;
#4
select * from emp order by hiredate asc ;
#5
select eno , ename , (sal + ifnull(comm,0)) as 'total salary' from emp where job !=
'manager';
#8
#1
alter table emp add city varchar(30) ;
#2
update emp set sal = sal*1.1 where job = 'manager' ;
#3
delete from emp where job = 'president' ;