project file 12th c (1)
project file 12th c (1)
Code:
def add(x,y):
return x+y
def subtract(x,y):
return x-y
def multipy(x,y):
return x*y
def divide(x,y):
return x/y
print("select operation")
print("1.add")
print("2.subtract")
print("3.multipy")
print("4.divide")
while True:
choice=input("enter choice(1/2/3/4):")
if choice in('1','2','3','4','5'):
try:
except ValueError:
continue
if choice=='1':
elif choice=='2':
elif choice=='3':
elif choice=='4':
if next_calculation=="no":
break
else:
print("invalid input")
print(“srishti”)
Output:
Program 2:
Code:
z=x-y
print(x,y,z)
c=a+b
print(c)
r=p+q
print(p,q,r)
subtract(100,20)
total(b=23,a=56)
product(56)
print("srishti")
Output:
Program 7:
Write a program to pass a list to function and add new data in it.
Code:
def data(l1):
l1.append(60)
l=[10,20,30,40,50]
data(l)
print("srishti")
Output:
Program 17 :
Code:
def calcsum(x,y):
z=x+y
return z
sum=calcsum(num1,num2)
print(“srishti”)
Output:
Program 3 :
Write a program to calculate size of a file.
Code :
f=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")
str=f.read()
size=len(str)
print(size,"bytes")
f.close()
Output:
Program 4:
Code:
f=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")
s=f.read()
p=s.split()
f.close()
print(”srishti”)
Output:
To count number of lines
Code:
F=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")
s=f.readlines()
f.close()
print(“srishti”)
Output:
Program 5 :
Write a program to display those lines which are starting with the vowel.
Code:
f=open("C:\\Users\\HP\\Desktop\\srishti.txt","r")
s=f.readlines()
print(s)
for i in s:
if i[0]in'aeiouAEIOU':
print(i)
f.close()
print(“srishti’’)
Output:
Program 6 :
By write operation
Code:
f=open('C:\\Users\\HP\\Desktop\\srishti1.txt','w')
f.write(s)
print('srishti')
f.close()
Output:
By writelines operation
Code:
f=open('C:\\Users\\HP\\Desktop\\srishti1.txt','w')
L=["HELLO","this","is","python"]
f.writelines(L)
print("srishi")
f.close()
def data(l1):
l1.append(60)
l=[10,20,30,40,50]
data(l)
print('srishti')
Output:
Program 9 :
Code:
import pickle
f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','wb')
s={'name':'nisha','number':123,'salary':1000}
s1={'name':'ankita','number':321,'salary':2000}
pickle.dump(s,f)
pickle.dump(s1,f)
f.close()
print("srishti")
Binary file:
Program 10 :
Code:
import pickle
f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','rb')
try:
while true:
s=pickle.load(f)
print(s)
except:
f.close()
print('srishti')
Program 12 :
Code:
import pickle
f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','rb')
try:
while True:
p=f.tell()
s=pickle.load(f)
if s['name']=='astha':
s['salary']=s['salary']+1000
f.seek(pos)
pickle.dump(s,f)
except:
f.close()
print('srishti')
Output:
Program 11 :
Write a program to search operation in a binary file.
Code:
import pickle
f=open('C:\\Users\\HP\\Desktop\\srishti1.dat','rb')
try:
while True:
s=pickle.load(f)
if s["salary"]>2000:
print(s["name"])
except:
f.close()
print("srishti")
Program 18 :
import csv
f=open("record.csv",'w')
a='y'
p=csv.writer(f)
while a=='y':
name=input("enter name")
rn=int(input("enter rollno."))
l=[name,rn,marks]
p.writerow(l)
a=input("if you want to enter a new record enter yes otherwise no")
f.close()
print("srishti")
Output:
Program 14 :
Code:
import csv
f=open("record.csv",'r')
r=csv.reader(f)
for i in r:
print(r)
print("srishti")
Output:
Program 15 :
1) push()
Code:
stk=()
top=None
def push():
if len(stk)==0:
stk.append(items)
top=top+1
print(top)
print("srishti")
Output:
2) pop()
stk=[19,39]
a=stk.pop()
print(a)
print(stk)
print("srishti")
Output:
Program 16 :
Write a program to display global and local variable.
Code:
#Global
def add(x,y=15):
global a
z=x+y
a=a+100
print(z)
print(a)
a=80
b=20
add(a,b)
Output:
#Local
def sub(a,b):
c=a-b
print(y,z)
print(c)
y=10
z=20
sub(y,z)
Output: