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

Record Programs 1 To 14

The document outlines 14 different Python programs, each with a specific aim such as searching elements in a list, manipulating values, managing a phone directory, file operations, and CSV file handling. Each program includes a brief description of its purpose, source code, and expected output. The programs cover various programming concepts including user-defined functions, file handling, and data manipulation.

Uploaded by

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

Record Programs 1 To 14

The document outlines 14 different Python programs, each with a specific aim such as searching elements in a list, manipulating values, managing a phone directory, file operations, and CSV file handling. Each program includes a brief description of its purpose, source code, and expected output. The programs cover various programming concepts including user-defined functions, file handling, and data manipulation.

Uploaded by

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

PROGRAM 1

AIM: Write a Python program to search an element in the list and display the frequency of element
present in the list and its location by using user defined function.

SOURCE CODE :

OUTPUT:

PROGRAM 2

AIM : Write a Python program to pass a list to a function, double the odd value, half the even value
and display the changed list.

SOURCE CODE:
OUTPUT :

PROGRAM 3

AIM : Write a Python program to pass a string to a function and count the vowels present in the
string.

SOURCE CODE:
OUTPUT :

PROGRAM 4

AIM : Write a Python program to create a phone directory of 5 friends’ name and phone number.
Create a menu driven program to perform the following :

1. Search for a particular name and print phone number


2. Print the name and phone numbers of those persons whose name starts with ‘A’ or ‘T’
3. Update phone number of a person
4. Print the entire dictionary
5. Exit

SOURCE CODE:
OUTPUT :
PROGRAM 5

AIM : Write a random number generator that generates random numbers between 1 and 6 (Simulates
a dice)

SOURCE CODE:
OUTPUT :

PROGRAM 6

AIM : Write a python program to copy all the lines starting with the character ‘A’ in a text file and
write it to another text file.

SOURCE CODE:
PROGRAM 7

AIM : Write a python program to read and display text file content line by line with each word
separated by #

SOURCE CODE:
PROGRAM 8

AIM : Write a python program to count frequency of words in a file.

SOURCE CODE:

PROGRAM 9

AIM :

Read a text file and display the number of


vowels/consonants/uppercase/lowercase
#characters in a text file.

SOURCE CODE:

file1=open("myfile.txt",'w')

str=input("Enter the text")

file1.write(str)

file1.close()

file1=open("myfile.txt",'r')

vowels=0

consonants=0

uppercase=0

lowercase=0

str1=file1.read()

for i in str1:

if(i>='a' and i<='z'):

lowercase+=1

elif (i>='A' and i<='Z'):

uppercase+=1

for j in str1:

j=j.lower()

if (j=='a' or j=='e' or j=='i' or j=='o' or j=='u' ):

vowels+=1

else:

if j.isalpha():

consonants+=1
print("lowercase count=",lowercase)

print("uppercase count=",uppercase)

print("vowels count=",vowels)

print("consonants count=",consonants)

PROGRAM 10

AIM :

Write a python program to implement a menu driven program to search a


particular record from a binary file.

SOURCE CODE:

import pickle
# write into a file
def write_file():
f=open('student.dat','ab')
roll=int(input('Enter the roll number: '))
name=input('Enter the name: ')
marks=int(input('Enter the marks: '))
data=[roll,name,marks]
pickle.dump(data,f)
f.close()

# Read from a file


def read_file():
f=open('student.dat','rb')
while True:
try:
rec=pickle.load(f)
print(rec)
except EOFError:
break
f.close()

# Search from file


def search_file():
f=open('student.dat','rb')
rn=int(input('Enter the roll number to be searched: '))
found=0
while True:
try:
rec=pickle.load(f)
if rec[0]==rn:
print(rec)
found=1
except EOFError:
break
f.close()
if found==0:
print('No such record.')

# Main def
while True:
print('1. Write to the file \n2. Read from the file \n3. Search a record \n4. Exit')
ch=int(input('Enter your choice: '))
if ch==1:
write_file()
elif ch==2:
read_file()
elif ch==3:
search_file()
elif ch==4:
break
else:
print('Wrong option selected.')

OUTPUT:
PROGRAM 11

AIM :

Write a python program to implement a menu driven program to update a


particular record from a binary file.

SOURCE CODE:

import pickle

#write into a file

def write_file():

f=open ("stu1.dat",'ab')

d={}
d['rollno']=int(input("enter the roll number:"))

d['name']=input("enter the name:")

d['marks']=int(input("enter the marks:"))

pickle.dump(d,f)

f.close()

#read from a file

def read_file():

f=open("stu1.dat",'rb')

while True:

try:

rec=pickle.load(f)

print(rec)

except EOFError:

break

f.close()

#update a record from file

def update_file():

f=open("stu1.dat",'rb+')

m=int(input("enter the roll number to be searched:"))

found=0

while True:

try:

pos=f.tell()

rec=pickle.load(f)

if d['rollno']==int(m):
d['name']==input("enter the new name:")

d['marks']==int(input("enter the new marks:"))

f.seek(pos)

pickle.dump(d,f)

found=1

except EOFError:

break

f.close()

if found==0:

print("no such record...")

#main def

d={}

while True:

print("1. write to the file\n 2. read from the file\n 3. update a record\n 4.
exit")

ch=int(input("enter your choice:"))

if ch==1:

write_file()

elif ch==2:

read_file()

elif ch==3:

update_file()

elif ch==4:

break

else:
print("wrong option selected")

PROGRAM 12

AIM :

Write a python program to implement a menu driven program to perform all file
operations in a binary file using dictionary.

SOURCE CODE:

#binary file operation using dict

#D ={'eid':23,'name':'Hari','Salary':4500}

import pickle

import OS

def write_b():

f=open("fil1.dat",'ab')

d={}

d['EID']=int(input("Enter the EID"))

d['Name']=input("Enter the name")

d['Salary']=int(input("Enter the salary"))

pickle.dump(d,f)

f.close()

def display():

f=open("fil1.dat",'rb')

while True:

try:
rec= picke.load(f)

print(rec)

except EOFError:

break

f.close()

def search ():

f=open("fil1.dat",'rb')

name = input("Enter the name of the searched")

found = 0

while True:

try:

rec=pickle.load(f)

if rec['Name']==name:

print(rec)

found=1

except EOFError:

f.close()

break

if found ==0:

print("No such name")

def del_b():

f=open("fil1.dat",'rb')

t=open("fil2.dat",'wb')

rn=int(input("Enter EID to be deleted"))

found = 0
while True:

try:

rec = pickle.load(f)

if rec['EID']!=rn:

pickle.dump(rec,t)

else:

found =1

except EOFError:

break

f.close()

t.close()

if found ==1:

OS.remove('fil1.dat')

OS.remove('fil2.dat')

else:

print("no such EID exists")

def update():

f=open("fil1.dat",'rb+')

eid = int(input("enter record to be updated"))

found = 0

while True:

try:

nos=f.tell()

rec = pickle.load(f)

if rec['EID'] == eid:
rec['Name']=input("Enter the new name")

rec['Salary']=int(input("Enter the new salary"))

found =1

print(rec)

f.seek(nos)

pickle.dump(rec,f)

except EOFError:

break

if found ==0:

print("No such record exists")

f.close()

#main

while True:

print("1.Write into file")

print("2.Display the contents of the file")

print("3.Search using name")

print("4.Search using EID")

print("5.Update using EID")

print("6.Exit")

ch=int(input("Enter your choice"))

if ch==1:

write_b()

if ch==2:

display()
if ch==3:

search()

if ch==4:

del_b()

if ch==5:

update()

if ch==6:

break

PROGRAM 13

AIM :

Write a python program to implement a menu driven program to write and


search from a csv file

SOURCE CODE:

import csv

def head():

f=open('cd.csv','w',newline='')

csv_w=csv.writer(f)

l=['cid','name','price']

f.close()

def write():

f=open('cd1.csv','a',newline='')

csv_w=csv.writer(f)

cid=int(input('CID: '))

name=input('Name: ')
price=int(input('Price: '))

l=[cid,name,price]

csv_w.writerow(l)

print('Record has been added')

f.close()

def read():

f=open('cd1.csv','r',newline='')

csv_r=csv.reader(f)

for rec in csv_r:

print(rec)

f.close()

def search():

f=open('cd1.csv','r',newline='')

csv_r=csv.reader(f)

cid=input('Enter CID to search: ')

found=0

for rec in csv_r:

if rec[0]==cid:

print(rec)

found+=1

f.close()

if found==0:

print('No records found')


head()

while True:

print()

print('1. Write into file \n2. Display Records \n3. Search for a record \n4. Exit')

ch=int(input('Enter choice: '))

if ch==1:

write()

elif ch==2:

read()

elif ch==3:

search()

elif ch==4:

break

else:

print('Invalid choice')
PROGRAM 14

AIM :

Write a python program to implement a menu driven program to update


particular record in a csv file :

SOURCE CODE:

#Update in CSV file

import csv

def head():

f=open('cd.csv','w',newline='')

csv_w=csv.writer(f)
l=['cid','name','price']

csv_w.writerow(l)

f.close()

def write():

f=open('cd1.csv','a',newline='')

csv_w=csv.writer(f)

cid=int(input('CID: '))

name=input('Name: ')

price=int(input('Price: '))

l=[cid,name,price]

csv_w.writerow(l)

print('Record has been added')

f.close()

def read():

f=open('cd1.csv','r',newline='')

csv_r=csv.reader(f)

for rec in csv_r:

print(rec)

f.close()

def update():

f=open('cd1.csv','r+',newline='')

csv_r=csv.reader(f)

cid=input('Enter CID to be updated: ')


found=0

l=[]

for rec in csv_r:

if rec[0]==cid:

rec[1]=input('Enter new name: ')

rec[2]=int(input('Enter the new price: '))

found+=1

l.append(rec)

if found==1:

f.seek(1)

csv_w=csv.writer(f)

for rec in l:

csv_w.writerow(rec)

print('Record has been updated')

else:

print('No record found')

f.close()

head()

while True:

print()

print('1. Write into file \n2. Display records \n3. Update a record \n4. Exit')

ch=int(input('Enter choice: '))

if ch==1:

write()

elif ch==2:
read()

elif ch==3:

update()

elif ch==4:

break

else:

print('Invalid choice')

You might also like