Computer First 5 Programs Edited
Computer First 5 Programs Edited
f=open("f1.txt","w")
n=int(input("Enter how many lines to be created:"))
for i in range(n):
t=input("Enter the line:")
f.write(t+"\n")
if (f):
print("File Created Successfully")
else:
print("Error Occured")
f.close()
def existing():
l=[]
f=open("f1.txt","a")
n=int(input("Enter how many lines to be created:"))
for i in range(n):
t=input("Enter the line:")
l.append(t+"\n")
f.writelines(l)
if (f):
print("Lines Appended Successfully")
else:
print("Error Occured")
f.close()
def read():
f=open("f1.txt","r")
data=f.read()
wds=data.split()
lines=data.split('\n')
print("No.of characters:",len(data))
print("No.of Words:",len(wds))
print("No.of Lines:",len(lines)-1)
f.close()
def main():
ans="y"
while ans.lower()=="y":
print('*'*25)
print("1.Perform Create A Text File")
print("2.Perform Read characters,Words And Lines in the Text File")
print("3.Perform Add Content To Existing File")
choice=int(input("Enter your choice:"))
if choice==1:
print('*'*25)
create()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
OUTPUT:
*************************
1.Perform Create A Text File
2.Perform Read characters,Words And Lines in the Text File
3.Perform Add Content To Existing File
Enter your choice:1
*************************
Enter how many lines to be created:5
Enter the line:The gentle breeze sways the trees, whispering secrets of the day.
Enter the line:Clouds drift lazily, painting the sky with soft hues of white and gray.
Enter the line:Birds chirp melodies, filling the air with a peaceful song.
Enter the line:Sunlight dapples through the leaves, warming the earth below.
Enter the line:Nature moves in quiet harmony, a timeless dance of life.
File Created Successfully
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A Text File
2.Perform Read characters,Words And Lines in the Text File
3.Perform Add Content To Existing File
Enter your choice:2
*************************
No.of characters: 317
No.of Words: 53
No.of Lines: 5
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A Text File
2.Perform Read characters,Words And Lines in the Text File
3.Perform Add Content To Existing File
Enter your choice:3
*************************
Enter how many lines to be created:2
Enter the line:The river hums softly as it winds along its path.
Enter the line:Mountains stand tall, guardians of the serene landscape.
Lines Appended Successfully
*************************
Would you like to continue:(y/n)n
if choice==2:
print('*'*25)
read()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==3:
print('*'*25)
existing()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
main()
def createf1():
f=open("f1.txt","w")
n=int(input("Enter how many lines to be created:"))
for i in range(n):
t=input("Enter the line:")
f.write(t+"\n")
if (f):
print("File Created Successfully")
else:
print("Error")
f.close()
def flettercopy():
f=open("f1.txt","r")
l=open("f2.txt","w")
ch=input("Enter The Letter Who's Corresponding Line To Be Copied:")
for i in f.readlines():
if ch==i[0]:
l.write(i)
if (f):
print("File Copied Successfully")
else:
print("Error")
f.close()
l.close()
def capital():
f=open("f1.txt","r")
l=open("f2.txt","w")
for i in f.readlines():
i=i.title()
l.write(i)
if (f):
print("File Copied Successfully")
else:
print("Error")
f.close()
l.close()
def reverse():
f=open("f1.txt","r")
l=open("f2.txt","w")
for i in f.readlines():
l.write(i[::-1])
if (f):
print("File Copied Successfully")
else:
print("Error")
f.close()
l.close()
def main():
ans="y"
while ans.lower()=="y":
print('*'*25)
print("1.Perform Create A Text File")
print("2.Perform Copying Lines Starting With Entered Letter")
print("3.Perform Capitalizing First Letter And Copying It")
print("4.Perform Reversing Words In A Text File And Copying It")
choice=int(input("Enter your choice:"))
if choice==1:
print('*'*25)
createf1()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==2:
print('*'*25)
flettercopy()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==3:
print('*'*25)
capital()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==4:
print('*'*25)
reverse()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
main()
OUTPUT:
*************************
1.Perform Create A Text File
2.Perform Copying Lines Starting With Entered Letter
3.Perform Capitalizing First Letter And Copying It
4.Perform Reversing Words In A Text File And Copying It
Enter your choice:1
*************************
Enter how many lines to be created:4
Enter the line:Sunlight dapples through the leaves, warming the earth below.
Enter the line:Birds chirp melodies, filling the air with a peaceful song.
Enter the line:The gentle breeze sways the trees, whispering secrets of the day.
Enter the line:The river hums softly as it winds along its path.
File Created Successfully
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A Text File
2.Perform Copying Lines Starting With Entered Letter
3.Perform Capitalizing First Letter And Copying It
4.Perform Reversing Words In A Text File And Copying It
Enter your choice:2
*************************
Enter The Letter Who's Corresponding Line To Be Copied:T
File Copied Successfully
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A Text File
2.Perform Copying Lines Starting With Entered Letter
3.Perform Capitalizing First Letter And Copying It
4.Perform Reversing Words In A Text File And Copying It
Enter your choice:3
*************************
File Copied Successfully
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A Text File
2.Perform Copying Lines Starting With Entered Letter
3.Perform Capitalizing First Letter And Copying It
4.Perform Reversing Words In A Text File And Copying It
Enter your choice:4
*************************
File Copied Successfully
*************************
Would you like to continue:(y/n)n
import pickle
def create():
f=open("traveldetails.dat","ab")
n=int(input("Enter how many travel tickets you want:"))
for i in range(n):
travel_id=int(input("Enter Travel ID:"))
start=input("Enter Start Journey:")
end=input("Enter End Journey:")
price=int(input("Enter Ticket Price:"))
L=[travel_id,start,end,price]
pickle.dump(L,f)
if (f):
print("File Created Successfully")
else:
print("Error Occured")
f.close()
def display():
f=open("traveldetails.dat","rb")
try:
print("ID","Start","End","Price",sep="\t")
while True:
det=pickle.load(f)
print(det[0],det[1],det[2],det[3],sep="\t")
except:
f.close()
def update(travel_id,price):
f=open("traveldetails.dat","rb+")
flag=False
try:
while True:
recpos=f.tell()
det=pickle.load(f)
if det[0]==travel_id:
det[-1]=price
f.seek(recpos)
pickle.dump(det,f)
flag=True
except:
if flag==False:
print("No Such Record Found")
else:
print("File Updated Successfully")
f.close()
def main():
ans="y"
while ans.lower()=="y":
print('*'*25)
print("1.Perform To Create A Binary File")
print("2.Perform To Display The File")
print("3.Perform Updating The File")
choice=int(input("Enter your choice:"))
if choice==1:
print('*'*25)
create()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==2:
print('*'*25)
display()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==3:
print('*'*25)
travel_id=int(input("Enter Travel ID To Be Updated:"))
price=int(input("Enter Ticket Price:"))
update(travel_id,price)
print('*'*25)
ans=input("Would you like to continue:(y/n)")
main()
OUTPUT:
*************************
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform Updating The File
Enter your choice:1
*************************
Enter how many travel tickets you want:4
Enter Travel ID:1
Enter Start Journey:DEL
Enter End Journey:HYD
Enter Ticket Price:4500
Enter Travel ID:2
Enter Start Journey:MAA
Enter End Journey:GOA
Enter Ticket Price:2700
Enter Travel ID:3
Enter Start Journey:HYD
Enter End Journey:MAA
Enter Ticket Price:1750
Enter Travel ID:4
Enter Start Journey:GOA
Enter End Journey:DEL
Enter Ticket Price:3200
File Created Successfully
*************************
Would you like to continue:(y/n)y
*************************
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform Updating The File
Enter your choice:2
*************************
ID Start End Price
1 DEL HYD 4500
2 MAA GOA 2700
3 HYD MAA 1750
4 GOA DEL 3200
*************************
Would you like to continue:(y/n)y
*************************
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform Updating The File
Enter your choice:3
*************************
Enter Travel ID To Be Updated:3
Enter Ticket Price:2500
File Updated Successfully
*************************
Would you like to continue:(y/n)n
import pickle
def create():
f=open("dictionary.dat","wb")
n=int(input("Enter how many words and their meanings you want:"))
for i in range(n):
word=input("Enter A Word:")
meaning=input("Enter Meaning Of The Word:")
d={word:meaning}
pickle.dump(d,f)
if (f):
print("File Created Successfully")
else:
print("Error Occured")
f.close()
def display():
f=open("dictionary.dat","rb")
print("WORD","MEANING",sep="\t")
try:
while True:
det=pickle.load(f)
for i in det:
print(i,det[i],sep="\t")
except:
f.close()
def search(word):
f=open("dictionary.dat","rb+")
try:
while True:
det=pickle.load(f)
for i in det:
if i==word:
print("Meaning of",word,":",det[i])
except:
f.close()
def main():
ans="y"
while ans.lower()=="y":
print('*'*25)
print("1.Perform To Create A Binary File")
print("2.Perform To Display The File")
print("3.Perform To Search The Meaning Of The Word In The File")
choice=int(input("Enter your choice:"))
if choice==1:
print('*'*25)
create()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==2:
print('*'*25)
display()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==3:
print('*'*25)
word=input("Enter The Word Whose Meaning Is To Be Searched:")
search(word)
print('*'*25)
ans=input("Would you like to continue:(y/n)")
main()
OUTPUT:
*************************
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform To Search The Meaning Of The Word In The File
Enter your choice:1
*************************
Enter how many words and their meanings you want:3
Enter A Word:Sunshine
Enter Meaning Of The Word:The light and warmth that comes from sun.
Enter A Word:Notebook
Enter Meaning Of The Word:A book of blank pages for writing notes.
Enter A Word:Strength
Enter Meaning Of The Word:The quality or state of being resilient.
File Created Successfully
*************************
Would you like to continue:(y/n)y
*************************
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform To Search The Meaning Of The Word In The File
Enter your choice:2
*************************
WORD MEANING
Sunshine The light and warmth that comes from sun.
Notebook A book of blank pages for writing notes.
Strength The quality or state of being resilient.
*************************
Would you like to continue:(y/n)y
*************************
1.Perform To Create A Binary File
2.Perform To Display The File
3.Perform To Search The Meaning Of The Word In The File
Enter your choice:3
*************************
Enter The Word Whose Meaning Is To Be Searched:Notebook
Meaning of Notebook : A book of blank pages for writing notes.
*************************
Would you like to continue:(y/n)n
import csv
def create():
with open("employe.csv","w",newline='')as f:
mywriter=csv.writer(f)
ans="y"
while ans.lower()=="y":
eid=int(input("Enter Employee ID:"))
ename=input("Enter Employee Name:")
esalary=int(input("Enter Employee Salary:"))
edesign=input("Enter Employee Designation:")
mywriter.writerow([eid,ename,esalary,edesign])
print("Data Written")
ans=input("Would you like to add more records:(y/n)")
def display():
f=open("employe.csv","r")
c=csv.reader(f)
print("ID","NAME","SALARY","DESIGNATION ",sep="\t")
for i in c:
print(i[0],i[1],i[2],i[3],sep="\t")
f.close()
def update(employid):
f=open("employe.csv","r+")
mywriter=csv.writer(f)
c=csv.reader(f)
for i in c:
if int(i[0])==employid:
print("Employee id:",i[0])
print("Employee Name:",i[1])
print("Employee Salary:",i[2])
print("Employee Designation:",i[3])
f.close()
def main():
ans="y"
while ans.lower()=="y":
print('*'*25)
print("1.Perform Create A CSV File")
print("2.Perform Displaying Employee Data")
print("3.Perform Searching Based On Employee ID")
choice=int(input("Enter your choice:"))
if choice==1:
print('*'*25)
create()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==2:
print('*'*25)
display()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==3:
print('*'*25)
employid=int(input("Enter Employee ID To Be Searched:"))
update(employid)
print('*'*25)
ans=input("Would you like to continue:(y/n)")
main()
OUTPUT:
*************************
1.Perform Create A CSV File
2.Perform Displaying Employee Data
3.Perform Searching Based On Employee ID
Enter your choice:1
*************************
Enter Employee ID:1
Enter Employee Name:Jack
Enter Employee Salary:20000
Enter Employee Designation:Sales
Data Written
Would you like to add more records:(y/n)y
Enter Employee ID:2
Enter Employee Name:James
Enter Employee Salary:1200000
Enter Employee Designation:Tech
Data Written
Would you like to add more records:(y/n)y
Enter Employee ID:3
Enter Employee Name:Sara
Enter Employee Salary:34000
Enter Employee Designation:Sales
Data Written
Would you like to add more records:(y/n)y
Enter Employee ID:4
Enter Employee Name:Evan
Enter Employee Salary:32000
Enter Employee Designation:Tech
Data Written
Would you like to add more records:(y/n)n
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A CSV File
2.Perform Displaying Employee Data
3.Perform Searching Based On Employee ID
Enter your choice:2
*************************
ID NAME SALARY DESIGNATION
1 Jack 20000 Sales
2 James 1200000 Tech
3 Sara 34000 Sales
4 Evan 32000 Tech
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A CSV File
2.Perform Displaying Employee Data
3.Perform Searching Based On Employee ID
Enter your choice:3
*************************
Enter Employee ID To Be Searched:3
Employee id: 3
Employee Name: Sara
Employee Salary: 34000
Employee Designation: Sales
*************************
Would you like to continue:(y/n)n
import csv
def create():
with open("stud.csv","w",newline='')as f:
mywriter=csv.writer(f)
n=int(input("Enter How Many Records You Want:"))
for i in range(n):
sid=int(input("Enter Student ID:"))
sname=input("Enter Student Name:")
s1 = int(input('Enter Subject 1 Mark: '))
s2 = int(input('Enter Subject 2 Mark: '))
s3 = int(input('Enter Subject 3 Mark: '))
s4 = int(input('Enter Subject 4 Mark: '))
s5 = int(input('Enter Subject 5 Mark: '))
s = s1+s2+s3+s4+s5
avg=s//5
mywriter.writerow([sid,sname,s,avg])
print("Data Written")
def display():
f=open("stud.csv","r")
c=csv.reader(f)
print("ID","NAME","SUM","AVERAGE",sep="\t")
for i in c:
print(i[0],i[1],i[2],i[3],sep="\t")
f.close()
def search(stuid):
f=open("stud.csv","r")
c=csv.reader(f)
for i in c:
if int(i[0])==stuid:
print("Sum:",i[2])
print("Average:",i[3])
f.close()
def main():
ans="y"
while ans.lower()=="y":
print('*'*25)
print("1.Perform Create A CSV File")
print("2.Perform Displaying Student Data")
print("3.Perform Searching Based On Student ID")
choice=int(input("Enter your choice:"))
if choice==1:
print('*'*25)
create()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==2:
print('*'*25)
display()
print('*'*25)
ans=input("Would you like to continue:(y/n)")
if choice==3:
print('*'*25)
stuid=int(input("Enter Student ID To Be Searched:"))
search(stuid)
print('*'*25)
ans=input("Would you like to continue:(y/n)")
main()
OUTPUT:
*************************
1.Perform Create A CSV File
2.Perform Displaying Student Data
3.Perform Searching Based On Student ID
Enter your choice:1
*************************
Enter How Many Records You Want:3
Enter Student ID:1
Enter Student Name:Jack
Enter Subject 1 Mark: 35
Enter Subject 2 Mark: 45
Enter Subject 3 Mark: 36
Enter Subject 4 Mark: 24
Enter Subject 5 Mark: 12
Data Written
Enter Student ID:2
Enter Student Name:James
Enter Subject 1 Mark: 56
Enter Subject 2 Mark: 89
Enter Subject 3 Mark: 34
Enter Subject 4 Mark: 56
Enter Subject 5 Mark: 90
Data Written
Enter Student ID:3
Enter Student Name:Evan
Enter Subject 1 Mark: 24
Enter Subject 2 Mark: 67
Enter Subject 3 Mark: 35
Enter Subject 4 Mark: 9
Enter Subject 5 Mark: 90
Data Written
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A CSV File
2.Perform Displaying Student Data
3.Perform Searching Based On Student ID
Enter your choice:2
*************************
ID NAME SUM AVERAGE
1 Jack 152 30
2 James 325 65
3 Evan 225 45
*************************
Would you like to continue:(y/n)y
*************************
1.Perform Create A CSV File
2.Perform Displaying Student Data
3.Perform Searching Based On Student ID
Enter your choice:3
*************************
Enter Student ID To Be Searched:2
Sum: 325
Average: 65
*************************
Would you like to continue:(y/n)n