Cs Record Python
Cs Record Python
COMPUTER SCIENCE
2025-2026
Aim:
To write a menu driven Python Program to perform Arithmetic operations
(+,-,*, /,//,%) based on the user’s choice.
Source Code:-
#Program for Arithmetic Calculator
result = 0
val1 = float(input("Enter the first value :"))
val2 = float(input("Enter the second value :"))
op = input("Enter any one of the operator (+,-,*,/,//,%)")
if op == "+":
result = val1 + val2
elif op == "-":
result = val1 - val2
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Please enter a value other than 0")
else:
result = val1 / val2
elif op == "//":
result = val1 // val2
else:
result = val1 % val2
print("The result is :",result)
#end of program
Result:
The above Python program was executed and the result was verified.
2. PERFECT NUMBER CHECK
Aim:
To write a program to check the given number is perfect or not.
Source Code:
def pernum(num):
divsum=0
for i in range(1,num):
if num%i == 0:
divsum+=i
if divsum==num:
print('Perfect Number')
else:
print('Not a perfect number')
return
#main
pernum(6)
pernum(15)
Result:
The above Python program was executed and the result was verified.
3. COUNT VOWELS, CONSONANTS, UPPERCASE, LOWERCASE CHARACTERS
Aim:
Source Code:
def countUL():
f =open(“sample.txt”,”r”)
str= f.read()
upper=lower=v=c=0
for ch in str:
if ch.isalpha():
if ch.lower() in ‘aeiou’:
v+=1
else:
c+=1
if ch.isupper():
upper+=1
if ch.islower():
lower+=1
print("Total no of uppercase = ",upper)
print("Total no of lowercase= ",lower)
print("Total no of vowels = ",v)
print("Total no of consonants= ",c)
f.close()
return
#main
countUL()
Result:
The above Python program was executed and the result was verified.
4. FIND THE OCCURRENCE OF GIVEN WORD IN A STRING
Aim:
To write a program to find the occurrence of the given word in a string.
Source Code:
def countWord(str1,word):
L = str1.split()
count=0
for w in L:
if w==word:
count+=1
return count
str1 = input("Enter any sentence :")
word = input("Enter word to search in sentence :")
count = countWord(str1,word)
if count==0:
print("## Sorry! ",word," not present ")
else:
print("## ",word," occurs ",count," times ## ")
#main
countWord(str1,word)
Result:
The above Python program was executed and the result was verified.
5. DOUBLE THE ODD VALUES AND HALF EVEN VALUES OF A LIST
Aim: -
To write a python program to pass list to a function and double the odd values
and half even values of a list and display list element after changing.
Source Code:-
def f(x):
newlist=[ ]
for i in x:
if i%2==0:
newlist.append(i//2)
else:
newlist.append(i*2)
print(“Modified list elements are:”,newlist)
return
#main
mylist=eval(input(“Enter the list elements:”))
print(“Original list elements are:”,mylist)
f(mylist)
Result:
The above Python program was executed and the result was verified.
6. PHISHING OF E-MAIL
Aim:
To write a program to find the most commonly occurring word(s) from the
sample of ten phishing e-mails (or any text file) and display the count of
the word.
Source Code:-
def countword():
file=open("email.txt","r")
content=file.read()
max=0
max_word=" "
wordlist=content.split()
for word in wordlist:
c=content.count(word)
if(c>max):
max=c
max_word =word
print("Most occuring word is:", max_word)
print("No of times “,max_word, “ occured is:",max)
file.close()
return
#main
countword()
OUTPUT
NOTE: Create a text file as “email.txt” and enter the below 10 emails.
“[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"luckyjackpot@americanlotter",
"[email protected]",
"[email protected]"
7. COUNT AND DISPLAY THE LINES STARTING WITH LETTER ‘T’
Aim:
To write a program to count and display the lines starting with letter “T” in the
given text file.
Source code:
def startwithT():
f=open("test.txt",'r')
linelist=f.readlines()
count=0
for line in linelist:
if line*0+.upper()==’T’:
count+=1
print(line)
print(“Total number of lines starts with T is “,count)
f.close()
return
#main
startwithT()
Result:
The above Python program was executed and the result was verified.
8. FIND THE OCCURRENCE OF THE WORDS ‘THIS’ AND ‘THAT’ IN A GIVEN FILE
Aim:
To write a program to find the occurrence of the words ‘this’ and ‘that’ in a given
text file.
Source code:
def countwords():
f=open('notes.txt','r')
this_count=that_count=0
str=f.read()
wordlist=str.split()
for i in wordlist:
if i.lower() == 'this' :
this_count+=1
if i.lower()=="that":
that_count+=1
print("Total no of occurrence of words this is ", this_count)
print("Total no of occurrence of words that is ", that_count)
f.close()
return
#main
countwords()
Result:
The above Python program was executed and the result was verified.
9. ODD AND EVEN NUMBERS COUNT IN TUPLE
Aim:-
To write a Python program to input n numbers in tuple and pass it to a
function and to count how many even and odd numbers are entered.
Source Code:-
def EOcount(x):
even=0
odd=0
for i in x:
if i%2==0:
even=even+1
else:
odd=odd+1
print(“Even count is:”,even)
print(“Odd count is:”,odd)
#main
t=eval(input("Enter the tuple elements"))
print("Tuple elements are = ", t)
EOcount(t)
Result:
The above Python program was executed and the result was verified.
10. UPDATE VALUE OF A KEY IN A DICTIONARY
Aim: -
To write a Python program to create and update a dictionary with the
given key and value entered by user.
Source Code:-
Output:-
Please enter the Key: 1
Please enter the Value: January
Updated Dictionary = {'1': 'January'}
Result:
The above Python program was executed and the result was verified.
11. REMOVE THE LINES THAT CONTAIN LETTER ‘A’ IN A FILE.
Aim:- To write a python program to remove all the lines that contain the
character ‘a’ in a file and write it to an another file.
Source Code:-
def remove_a():
f=open("sample.txt",'r')
x=f.readlines()
f.close()
f1=open("x.txt",'w')
f2=open("y.txt",'w')
for i in x:
if 'a' in i:
f2.write(line)
else:
f1.write(line)
print("Lines containing character a was removed from x.txt file")
print("Lines containing character a was saved in y.txt file")
f1.close()
return
#main
remove_a()
Result:
The above Python program was executed and the result was verified.
12. DISPLAY EACH WORD SEPARATED BY # FROM A TEXT FILE
Aim: -
To write a python program to read and display file content line by
line with each word separated by #.
Source Code:-
f=open(“sample.txt”,’r’)
L=f.readlines()
for line in L:
for word in line.split():
print(word+ “#” , end=” “)
print(“ “)
f.close()
Result:
The above Python program was executed and the result was verified.
Source Code:
import random
n=random.randint(1,6)
guess=int(input("Enter a number between 1 to 6 :"))
if n==guess:
print("Congratulations, You won the lottery ")
else:
print("Sorry, Try again, The lucky number was : ", n)
Result:
The above Python program was executed and the result was verified.
14. BINARY FILE – SEARCH AND DISPLAY A STUDENT RECORD
Aim:
To write a Python Program to Create a binary file with roll number, name and
mark. Search for a given roll number and display the student details, if not found
display appropriate message.
Source Code:
def create():
student=[]
f=open('student.dat','ab')
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
pickle.dump(student,f)
f.close()
def display():
f=open('student.dat','rb')
student=[ ]
while True:
try:
student = pickle.load(f)
except EOFError:
break
found=False
r = int(input("Enter Roll number to display record :"))
for s in student:
if s[0]==r :
print("RollNo: ",s[0])
print("Name :",s[1])
print("Mark :",s[2])
found=True
if found==False:
print("####Sorry! Roll number not found ####")
f.close()
#main
import pickle
ch=None
while ch!=3:
print("1. ADD RECORD")
print("2. DISPLAY RECORD")
print("3.EXIT")
ch=int(input("Enter 1 to add record / 2 to display record / 3 to exit "))
if ch==1:
create()
elif ch==2:
display()
elif ch==3:
print("thank you")
break
else:
print("Invalid entry")
Result:
The above Python program was executed and the result was verified.
15. BINARY FILE - UPDATE STUDENT DETAILS
Aim:
To write a Python Program to Create a binary file with roll number, name, mark
and update/modify the mark for the given roll number.
Source Code:
def create():
student=[]
f=open('student_det.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)")
pickle.dump(student,f)
f.close()
return
def update():
f=open('student_det.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 update :"))
for s in student:
if s[0]==r :
print("Name is :",s[1])
print("Current Mark is :",s[2])
m = int(input("Enter new marks :"))
s[2]=m
print("Marks updated")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
#writing updated record in file
if found==True:
f = open('student_det.dat','wb')
for s in student:
pickle.dump(s,f)
f.close()
return
#main
import pickle
ch=None
while ch!=3:
print("1. ADD RECORD")
print("2. UPDATE RECORD")
print("3.EXIT")
ch=int(input("Enter 1 to add record / 2 to delete record / 3 to exit "))
if ch==1:
create()
elif ch==2:
update()
elif ch==3:
print("thank you")
break
else:
print("Invalid entry")
Result:
The above Python program was executed and the result was verified.
16. BINARY FILE – SEARCH AND DELETE A STUDENT RECORD
Aim: To write a Python Program to Create a binary file with roll number, name,
mark and delete the student record for the given roll number.
Source Code:
def create():
student=[]
f=open('student_det.dat','ab')
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
pickle.dump(student,f)
f.close()
return
def delete():
f=open('student_det.dat','rb')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
f.close()
break
found=False
f=open('student_det.dat','wb')
r = int(input("Enter Roll number to delete record :"))
for s in student:
if s[0]==r :
print("RollNo: ",s[0])
print("Name :",s[1])
print("Mark :",s[2])
found=True
print("Record is deleted")
continue
pickle.dump(s,f)
if found==False:
print("####Sorry! Roll number not found ####")
f.close()
return
#main
import pickle
ch=None
while ch!=3:
print("1. ADD RECORD")
print("2. DELETE RECORD")
print("3.EXIT")
ch=int(input("Enter 1 to add record / 2 to delete record / 3 to exit "))
if ch==1:
create()
elif ch==2:
delete()
elif ch==3:
print("thank you")
break
else:
print("Invalid entry")
Result:
The above Python program was executed and the result was verified.
17. CSV FILE - WRITE AND READ EMPLOYEE DETAILS
Aim: To write a Python program Create a CSV file to store Empno , Name and
Salary and display the employee details.
Source Code:
#function to write record into csv file
def writecsv():
with open("employee.csv", 'w', newline='') as fobj:
mywriter = csv.writer(fobj, delimiter = ',')
# write new record in file
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?")
#function to read record from csv file
def readcsv():
with open("employee.csv", 'r') as f:
data = csv.reader(f)
# reader function to generate a reader object
for row in data:
print(row)
#main function
import csv
ans='y'
while ans=='y':
print("Press-1 to Write Data and Press-2 to Read data: ")
a = int(input())
if a == 1:
writecsv()
elif a == 2:
readcsv()
else:
print("Invalid value")
ans= input("Wish to continue(y/n)?")
Result:
The above Python program was executed and the result was verified.
18. CSV FILE – SEARCH AND DISPLAY EMPLOYEE DETAILS
Aim: To write a Python program Create a CSV file to store Empno, Name, Salary
and search any Empno and display Name, Salary and if not found display
appropriate message.
Source Code:
def create():
f=open("e:\\myfile.csv","a”, newline="")
x=csv.writer(f)
n=int(input("Enter the number of records:"))
for i in range(n):
eno=int(input("Enter Employee Number:"))
ename=input("Enter Employee Name:")
salary=int(input("Enter Employee Salary:"))
y=[eno,ename,salary]
x.writerow(y)
f.close()
return
def display():
f=open("e:\\myfile.csv","r")
x=csv.reader(f)
while True:
n=int(input("Enter Employee Number to Search:"))
for i in x:
if int(i[0])==n:
print("NAME :",i[1])
print("SALARY :",i[2])
break
else:
print(" Data not found")
break
f.close()
return
#main
import csv
ans='y'
while ans=='y':
print("Press-1 to Write Data and Press-2 to display data: ")
a = int(input())
if a == 1:
create()
elif a == 2:
display()
else:
print("Invalid value")
ans= input("Wish to continue(y/n)?")
Result:
The above Python program was executed and the result was verified.
19. READ THE PASSWORD IN A CSV FILE
Aim: - To create a CSV file by entering user-id and password, read and
search the password for given userid.
Source Code: -
def writepwd():
f=open("e:\\mycsvfile.csv","a", newline="")
x=csv.writer(f)
n=int(input("Enter the number of records:"))
for i in range(n):
uid=input("Enter User:")
pd=input("Enter Password:")
y=[uid,pd]
x.writerow(y)
f.close()
def searchpwd():
f=open("e:\\mycsvfile.csv",'r')
n=input("Enter uid to search pwd:")
x=csv.reader(f)
for i in x:
if i[0]==n:
print("Your Password is:",i[1])
break
else:
print("Sorry data not found")
f.close()
#main
import csv
writepwd()
searchpwd()
Output:-
Enter the number of records:2
Enter Userid: admin1
Enter Password:111
Enter Userid: admin2
Enter Password:222
Enter uid to search pwd: admin1
Your Password is: 111
Enter uid to search pwd: admin3
Sorry data not found
Result:
The above Python program was executed and the result was verified.
20. STACK OPERATIONS
Source Code:
Result:
The above Python program was executed and the result was verified.