Que 1 : Write a program to create calculator using
functions add, sub , divide, multiply , square .
Code:
s=0
def add():
a=int(input(“enter a no.”))
b=int(input(“enter a no.”))
s=a+b
print(s)
def subtract():
a=int(input(“enter a no.”))
b=int(input(“enter a no.”))
s=a-b
print(s)
def multiply():
a=int(input(“enter a no.”))
b=int(input(“enter a no.”))
s=a*b
print(s)
def divide():
Que 2: write a program to implement
different types of functions argument.
code:
def sum(x,y): # positional argument
z=x+y
print(x,y,z)
def total (a,b):# keyword argument
c=a+b
print(c)
def product(p,q=84):# Default parameter
r=p+q
print(p,q,r)
sum(2,12)
total(b=12,a=34)
product(56)
print(“NEHA ”)
Que : 3 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]
print(‘list before adding new data is’,l)
data(l)
print(“list before adding new data is”,l)
print(“NEHA”)
Que 4 : write a program to implement LEGB
rule.
Code:
def data (l1):
l1.append(60)
l=[10,20,30,40,50]
print(‘list before adding new data is’,l)
data(l)
print(“list before adding new data is”,l)
print(“NEHA”)
Que 7(a): write a program to calculate size
of a file.
Code:
f=open(‘file1.txt’, ‘r’)
s=f.read()
print(s)
print(len(s))
print(“NEHA”)
f.close()
(b) Write a program to count number of words
and number of lines in a file.
To count number of words
code:
f=open(‘file1.txt’, ‘r’)
s=f.read()
p=s.split()
print(s)
print(“total number of words in the file is” , len(p))
print(“NEHA”)
f.close()
To count number of lines
Code:
f=open(‘file1.txt’, ‘r’)
s=f.readlines()
print(s)
print(“total number of lines in the file is” , len(s))
print(“NEHA”)
f.close()
(c) Write a program to display those lines
which are starting with the vowel.
Code:
f=open(‘file1.txt’, ‘r’)
s=f.readlines()
print(s)
for i in s:
if i[0]in ‘aeiouAEIOU’:
print(i)
print(“NEHA”)
f.close()
(d) Write a program to perform write
operation on text file using both operation .
By write operation
Code:
f=open(‘file1.txt’, ‘r’)
s=‘Good morning Everyone’
f.write(s)
print(“NEHA”)
f.close()
By writelines operation
code:
f=open(‘file1.txt’, ‘w’)
L=[“HELLO” , “this” , “ is” , “python”)
f.writelines(L)
Print(“NEHA”)
f.close()
Que 8 : write a program to perform write
operation on text file.
Code:
f=open(‘file1.txt’, ‘w’)
s=‘Good morning Everyone’
f.write(s)
print(“neha”)
f.close()
(b)Write a program to perform read
operation on binary file.
Code:
Import pickle
f=open(‘file2.dat’ , ‘rb’)
try:
while true:
s=pickle.load(f)
print(s)
except:
f.close()
print(“NEHA”)
(c) Write a program to update binary file.
Code:
Import pickle
f=open(‘file2.dat’ , ‘rb’)
try:
while true:
p=f.tell()
s=pickle.load(f)
if s[‘name’]==‘ashi’:
s[‘salary’]=s[‘salary’]+1000
f.seek(p)
pickle.dump(s,f)
except:
f.close()
print(“NEHA”)
Que: write a program to search operation in
file.
Import pickle
f=open(“rat.dat”, “rb”)
try:
while True:
s=pickle.load(f)
if s[“salary”]>20000:
print(s[“name”])
except:
f.close()
print(“Radhika”)
(d) Write a program to search on binary file.
Code:
Import pickle
f=open(“rat.dat”, “rb”)
try:
while True:
s=pickle.load(f)
if s[“salary”]>20000:
print(s[“name”])
except:
f.close()
print(“NEHA”)
Que: write a program to operate on csv file.
import csv
f=open("record.csv" ,'w')
a='y'
p=csv.writer(f)
while a=='y':
name=input("enter name")
rn=int(input("enter roll no."))
marks=int(input("enter your marks"))
l=[name,rn,marks]
p.writerow(l)
a=input("if you want to enter a new record enter yes otherwise no")
f.close()
Que: write a program to read a binary file.
import csv
f=open("record.csv",'r')
r=csv.reader(f)
for i in r:
print(r)
print(“neha")
Que: WAP to implement stack operation
1) push()
stk=()
top=None
def push():
if len(stk)==0:
stk.append(items)
top=top+1
print(top)
print(“neha")
2)pop()
stk=[12,34]
a=stk.pop()
print(a)
print(stk)
print(“neha“)
Que: write a program to display global and
local variable.
def add(x,y):
global a
z=x+y
a=a+10
print(z)
print(a)
a=12
b=12
add(a,b)
Local:-
def add(x,y):
z=x+y
print(a,b)
print(z)
a=12
b=19
add(a,b)
print(“NEHA")