0% found this document useful (0 votes)
16 views10 pages

Lab Programs SAB

Uploaded by

adehcm2021tsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Lab Programs SAB

Uploaded by

adehcm2021tsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab programs

1.Arithmatic calculator using functions:


# define functions to add,mul,div,floodiv,moddiv
def add(x,y):
return x+y
def subtract(x,y):
return x-y
def multiply(x,y):
return x*y
def divide(x,y):
return x/y
def floordiv(x,y):
return x//y
def moddiv(x,y):
return x%y
#take input from the user
print ("select operation")
print("1.add")
print("2.subract")
print("3.multiply")
print("4.divide")
print("floor division")
print("6.modulo division")
choice=input ("enter choice(1/2/3/4/5/6):")
while choice<'7':
num1=int(input("enter first number:"))
num2=int (input("enter second number:"))
if choice =='1':
print(num1,"+",num2,"=",add(num1,num2))
elif choice=='2':
print (num1,"_",num2,"=",subract(num1,num2))
elif choice =='3':
print (num1,"*",num2,"=",multiply(num1,num2))
elif choice=='4':
print(num1,"/",num2,"=",divide(num1,num2))
elif choice =='5':
print (num1,"//",num2,"=",floordiv(num1,num2))
elif choice =='6':
print (num1,"%",num2,"=",moddiv(num1,num2))
choice =input ("enter choice(1/2/3/4/5/6):")
print("exit")

output:
select operation
1.add
2.subract
3.multiply
4.divide
floor division
6.modulo division
enter choice(1/2/3/4/5/6):3
enter first number:2
enter second number:9
2 * 9 = 18
enter choice(1/2/3/4/5/6):9
exit

=== Code Execution Successful ===

2.To find total number of vowels, consonants,


lowercase and uppercase characters:
f= open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data =f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower()in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=''and ch!='\n':
o+=1
print("Total vowels:",v)
print("Total consonants in file:",c)
print("Total capital letters:",u)
print("Total small letters:",l)
print ("Total other than letters:",o)
f.close()

output:
Total vowels:20
Total consonants in file :29
Total capital letters in file:8
Total small letters in file:46
Total other than letters:33

===Code Execution Successful===

3.To copy a text file except for those line contains


letter “a”:
f1=open("file1.txt")
f2=open ("file1copy.txt","w")
for line in f1:
if 'a'not in line:
f2.write(line)
print ("## file copied successfully!## ")
f1.close()
f2.close()
output:
## file copied successfully!##

===Code Execution Successful===

4.To update the mark of a student in binary file:


import pickle
student=[]
#Binary file opened as Append and Write mode
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll=int(input("Enter roll number:"))
name=input("Enter name:")
marks=int(input("Enter marks:"))
student.append([roll,name,marks])
ans=input("Add more?(Y/N)")
pickle.dump(student,f)
f.close()
#Binary file opened as read mode
f=open('student.dat','rb+')
student=[]
while True:
try:
student=pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r=int(input("enter roll number to serch:"))
for s in student:
if s[0]==r:
print ("name is:",s[1])
print("current marks is:",s[2])
m=int(input("Enter new marks:"))
s[2]=m
print("A2fter updating the mark is:",s[2])
found=True
break
if not found:
print("##sorry! roll number not found##")
ans=input("update more?(Y/N):")
f.close()

output:
Enter roll number:1
Enter name: Sabz
Enter marks: 95
Add more? (Y/N) Y
Enter roll number :2
Enter name: Aakz
Enter marks: 98
Add more? (Y/N) Y
Enter roll number: 3
Enter name: Deez
Enter marks:97
Add more? (Y/N) N
Enter roll number to be searched:3
Name is: Deez
Current marks: 97
Enter new marks :94
After updating the mark is 94
Update more? (Y/N): N

===Code Execution Successful===

5.To display EMP name and salary in csv file:


import csv
with open ('myfile1.csv',mode='a') as csvfile:
mywriter=csv.writer (csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
eno=int(input("Enter employee number:"))
name=input("Enter employee name:")
salary=int(input("Enter employee salary:"))
mywriter.writerow([eno,name,salary])
print("##Data saved...##")
ans=input("Add more?")
ans='y'
with open('myfile1.csv',mode='r') as csvfile:
while ans.lower()=='y':
myreader=csv.reader(csvfile,delimiter=',')
found=False
e=int(input("Enter employee number to search:"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("==========================")
print("NAME:",row[1])
print("SALARY:",row[2])
found=True
break
if not found:
print("=========================")
print("EMPNO NOT FOUND")
print("=========================")
csvfile.seek(0)
ans=input("search more?(Y)")

output:
Enter employee number: 001
Enter employee name: Sivz
Enter employee salary: 64000
##Data saved…##
Add more? (Y/N) Y
Enter employee number: 002
Enter employee name: Ramz
Enter employee salary: 50000
##Data saved…##
Add more? (Y/N) N
Enter employee number to be searched: 002
=========================
NAME: Ramz
SALARY: 50000
Enter employee number to be searched: 003
=========================
EMPNO NOT FOUND
==========================
Search more? (Y/N) N

===Code Execution Successful===


Vedic Vidyashram Senior Secondary School
Computer Assignment [2024-25]
Lab Programs

NAME : C.SABARISH
CLASS : XII – ‘A’

You might also like