PRW10 557
PRW10 557
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
1. Aim : Write a Python Program to count the occurences of a character from a file.
Program :
s=input("Enter a letter : ")
fp=open("sample1.txt","r")
count=0
for line in fp:
for n in range(0,len(line)) :
if(line[n]==s) :
count=count+1
fp.close()
print("The letter ",s," appeared ",count," times in the file.")
Output :
Enter a letter : d
The letter d appeared 12 times in the file.
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
2. Aim : Write a python program to print percentage of vowels and consonants in a file.
Program :
fp=open("sample1.txt","r")
c1=0
c2=0
c3=0
for line in fp:
c1=c1+len(line)
for char in line :
if char in "aeiouAEIOU" :
c2=c2+1
if char in "bcdfghjklmnpqrstvwxyzBCDFGHIJKLMNPQRSTVWXYZ" :
c3=c3+1
fp.close()
print("Count of Total characters in the file : ",c1)
print("Count of Vowel characters : ",c2)
print("Count of Consonant characters : ",c3)
a=(c2/c1)*100
b=(c3/c1)*100
print("Percentage of vowels in the file : %.2f"%a,end="")
print("%")
print("Percentage of consonants in the file : %.2f"%b,end="")
print("%")
Output :
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
Program :
fp=open("sample2.txt","r")
c=0
d={}
for line in fp:
s=line.split()
for str in s:
if str in d :
d[str]+=1
else :
d[str]=1
fp.close()
print("The dictionary is : ",d)
Output:
The dictionary is : {'You’re': 2, 'out': 4, 'of': 4, 'order!': 4, 'The': 1, 'whole': 1, 'trial': 1, 'is':
1, 'They’re': 1}
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
4. Aim : Write a python program to print sum and average of ‘n’ student marks in ‘n’
students containing in a file.
Program :
fp=open("grades.txt","r")
num=int(fp.readline())
print("The total no.of students : ",num,"\n")
for i in range(num) :
y=fp.readlines()
l=1
for line in y :
l2=line.split()
l1=list(l2)
print("Student #",l," : ",l2)
sum=0
for s in l1 :
sum=sum+int(s)
print("The sum is : ",sum)
avg=(sum/500)*100
print("The percentage is ",avg,"%\n")
l=l+1
Output :
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
Program :
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
6. Aim : Write a Python GUI program using breezypythongui library, to demonstrate
text fields. Convert the given text to upper case.
Program :
from breezypythongui import EasyFrame
class TextFieldDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, title = "Text Field Demo")
self.addLabel(text = "Enter a string", row = 0, column = 0)
self.inputField = self.addTextField(text = "",row = 0,column = 1)
# Label and field for the output
self.addLabel(text = "String in uppercase", row = 1, column = 0)
self.outputField = self.addTextField(text = "",row = 1,column = 1,state =
"readonly")
# The command button
self.addButton(text = "Convert", row = 2, column = 0,
columnspan = 2, command = self.convert)
# The event handling method for the button
def convert(self):
text = self.inputField.getText()
result = text.upper()
self.outputField.setText(result)
def main():
"""Instantiates and pops up the window."""
TextFieldDemo().mainloop()
if __name__ == "__main__":
main()
Output :
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
Program :
Inference :
Week-10 Roll No. 21331A0557
Developed by : G.Sai Hemanth Kumar
Date : 13-12-2022
8. Aim : Write a Python GUI program using breezypythongui library, to demonstrate
integer and float fields. Calculate the square root of the given number. Handle
exceptions and display an error message if the user gives a negative number as input.
Program :
from breezypythongui import EasyFrame
import math
class NumberFieldDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, title = 'Number Field Demo')
self.addLabel(text = "An integer", row = 0, column = 0)
self.inputField = self.addIntegerField(value = 0, row = 0, column = 1,width = 10)
self.addLabel(text = "Square root", row = 1, column = 0)
self.outputField = self.addFloatField(value = 0.0, row = 1,column = 1,width =
8,precision = 2, state = "readonly")
self.addButton(text = "Compute", row = 2, column = 0,columnspan = 2, command
= self.computeSqrt)
def computeSqrt(self):
try:
number = self.inputField.getNumber()
result = math.sqrt(number)
self.outputField.setNumber(result)
except ValueError:
self.messageBox(title = "ERROR", message = "Input must be an integer >= 0")
def main():
NumberFieldDemo().mainloop()
if __name__ == "__main__":
main()
Output :
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022
Inference :
END OF THE WEEK : 10
Concepts Covered :
1. Creating a label.
2. Creating a button.
3. Implementing counter button.
4. Square root computer.
5. Textfield method.
6. Investment calculator.
GRADE : _________
Signature of Lab-Incharge : ____________________