Professional Documents
Culture Documents
PRACTICAL 1:
Write a program to create a dictionary containing names of
competition winner students as keys and number of their
wins as values.
n=int(input("How many students are there?"))
CompWinners={}
for i in range(n):
key=input("Enter the name of the student:")
value=int(input("Enter the number of the competitions
won:"))
CompWinners[key]=value
print("Your current dictionary is:")
print(CompWinners)
OUTPUT:
6
PRACTICAL 2:
Write a program that receives two numbers in a function
and return of all arithmetic operations (+, -, *, /, %) on the
number.
def arithmetic(a,b):
p=a+b
print("The sum of your two numbers are:", p)
q=a-b
print("The difference between your two numbers are:", q)
r=a*b
print("The product of your two numbers are:", r)
s=a/b
print("The quotient of your two numbers are:", s)
t=a%b
print("The modulus of your two numbers are:", t)
#main code
x=int(input("Enter your first number:"))
y=int(input("Enter your second number:"))
arithmetic(x,y)
OUTPUT:
7
PRACTICAL 3:
Write a random number generator that generates random
numbers between 1 and 6 (Simulates a dice).
import random
min=1
max=6
roll_again="y"
while roll_again=="y" or roll_again=="Y":
print("Rolling the dice...")
val=random.randint(min,max)
print("You get...",val)
roll_again=input("Roll the dice again? (y/n)...")
OUTPUT:
8
PRACTICAL 4:
Write a program to get roll no., marks, name of the
students of a class and store these details in a file called
'Marks.txt'.
count=int(input("How many students are there in the
class?"))
fileout=open("Marks.txt","w")
for i in range(count):
print("Enter details for student",(i+1),"below:")
rollno=int(input("Enter the roll no.:"))
name=input("Enter the name:")
marks=float(input("Enter the marks:"))
rec=str(rollno)+","+name+","+str(marks)+'\n'
fileout.write(rec)
fileout.close()
9
OUTPUT:
1. Spyder (Python 3.8):
PRACTICAL 5:
Write a program to read a text file line by line and display
each word separated by a '#' symbol.
myfile=open("answer.txt","r")
line="a"
while line:
line=myfile.readline()
for word in line.split():
print(word, end='#')
print()
myfile.close()
OUTPUT:
11
PRACTICAL 6:
Write a program to read a text file and display the count of
vowels and consonants in the file.
myfile=open(r"D:/ComputerPracticals/answer.txt","r")
ch="a"
vcount=0
ccount=0
while ch:
ch=myfile.read(1)
if ch in ['a','A','e','E','i','I','o','O','u','U']:
vcount+=1
else:
ccount+=1
print("Vowels in the file:",vcount)
print("Consonants in the file:",ccount)
myfile.close()
12
OUTPUT:
13
PRACTICAL 7:
Write a program to create a CSV file to store student data
(Roll no., Name, Marks). Obtain the data from the user and
write 5 records into the file.
import csv
myfile=open("Students.csv","w")
stuwriter=csv.writer(myfile)
stuwriter.writerow(['Rollno','Name','Marks'])
for i in range(5):
print("Student record",(i+1))
rollno=int(input("Enter roll no.:"))
name=input("Enter name:")
marks=float(input("Enter marks:"))
sturec=[rollno, name, marks]
stuwriter.writerow(sturec)
myfile.close()
14
OUTPUT:
1. Spyder (Python 3.8):
PRACTICAL 8:
Write a program to remove all the lines that contains
character 'a' in a file and write it into another file.
def remove():
myfile=open(r"D:\Computer Practicals\SampleOld.txt","r")
lines=myfile.readlines()
oldfile=open("SampleOld(1).txt","w")
newfile=open("SampleNew.txt","w")
for line in lines:
if 'a' in line:
newfile.write(line)
else:
oldfile.write(line)
print("Data updated in Sample Old File...and created Sample
New File also...")
remove()
OUTPUT:
1. Spyder (Python 3.8):
PRACTICAL 9:
Write a program to create a binary file with name, roll no.
and marks, and input the roll no. and update the marks.
import pickle
StuDat={}
def store_it(n):
print("Store your marks here:")
myfile=open("StuDat.txt","wb")
for i in range(n):
rno=int(input("Enter the roll no.:"))
name=input("Enter the name:")
marks=float(input("Enter the marks:"))
StuDat['Roll no.']=rno
StuDat['Name']=name
StuDat['Marks']=marks
pickle.dump(StuDat,myfile)
def update_it():
print("Update your marks here:")
found=0
myfile=open("StuDat.txt","rb+")
rno=int(input("Enter the roll no.:"))
try:
while True:
pos=myfile.tell()
StuDat= pickle.load(myfile)
if StuDat["Roll no."]==rno:
NewMarks=float(input("Enter the marks:"))
myfile.seek(pos)
18
StuDat["Marks"]==NewMarks
pickle.dump(StuDat,myfile)
found=1
except EOFError:
if found==0:
print("Roll no. not found...")
else:
print("Student marks updated")
#main code
store_it(3)
update_it()
19
OUTPUT:
1. Spyder (Python 3.8):
PRACTICAL 10:
Create a binary file with name and roll no. Search for a
given roll no. and display the name if not found. Display an
appropriate message.
import pickle
def write_it():
f=open("Students.dat","wb")
while True:
r=int(input("Enter roll no.:"))
n=input("Enter name:")
Data=[r,n]
pickle.dump(Data,f)
ch=input("More? (y/n):")
if ch in 'Nn':
break
def read_it():
f=open("Students.dat","rb")
try:
while True:
rec=pickle.load(f)
print(rec)
except EOFError:
f.close()
def search_it():
found=0
rno=int(input("Enter the roll no. whose name you want to
search:"))
f=open("Students.dat","rb")
try:
21
while True:
rec=pickle.load(f)
if rec[0]==rno:
print(rec[1])
found=1
break
except EOFError:
f.close()
if found==0:
print("Sorry....No record found....")
#main code
write_it()
read_it()
search_it()
22
OUTPUT:
23
PRACTICAL 11:
Write a program to enter the temperature in Celsius and
convert to Fahrenheit using function.
def temperature(c):
print("Converting the given temperature (in
Celsius) to Fahrenheit:")
f=(c*(9/5))+32
print("The temperature in Fahrenheit is:",f)
n=int(input("Enter the temperature (In Celsius):"))
#main code
temperature(n)
OUTPUT:
24
PRACTICAL 12:
Write a program using a function to print Fibonacci
sequence upto n numbers.
def fib(n):
if n==1:
return 0
elif n==2:
return 1
else:
return fib(n-1)+fib(n-2)
n=int(input("Enter last term required:"))
for i in range(1,n+1):
print(fib(i), end=',')
print("....")
OUTPUT:
25
PRACTICAL 13:
Write a program to know the cursor position and print the
text according to below-given specifications:
Print the initial position
Move the cursor to 4th position
Display next 5 characters
Move the cursor to the next 10 characters
Print the current cursor position
Print next 10 characters from the current cursor position
def position():
f=open(r"D:/Computer Practicals/Merge.txt","r")
print(f.tell())
f.seek(4,0)
print(f.read(5))
f.seek(10,0)
print(f.tell())
print(f.seek(7,0))
print(f.read(10))
position()
26
OUTPUT:
27
PRACTICAL 14:
Create a text file “MyFile.txt” in python and ask the user to
write separate 3 lines with three input statements from the
user.
def linewise():
myfile=open("MyFile.txt","w")
line1=input("Enter the text:")
line2=input("Enter the text:")
line3=input("Enter the text:")
new_line='\n'
myfile.write(line1)
myfile.write(new_line)
myfile.write(line2)
myfile.write(new_line)
myfile.write(line3)
myfile.write(new_line)
myfile.close()
linewise()
28
OUTPUT:
1. Spyder (Python 3.8):
PRACTICAL 15:
Write a program to replace all spaces from text with –
(dash).
def read_it():
cnt=0
n=int(input("Enter the no. of characters to read:"))
with open(r"D:/Computer Practicals/Merge.txt","r") as f1:
data=f1.read()
data=data.replace("","-")
with open(r"D:/Computer Practicals/Merge1.txt","w") as f1:
f1.write(data)
read_it()
OUTPUT:
1. Spyder (Python 3.8):