0% found this document useful (0 votes)
20 views22 pages

PRW10 557

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

PRW10 557

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

WEEK-10

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 :

Count of Total characters in the file : 277


Count of Vowel characters : 80
Count of Consonant characters : 134
Percentage of vowels in the file : 28.88%
Percentage of consonants in the file : 48.38%

Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022

3. Aim : Write a python program to convert words of a file into dictionary of


occurances.

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 :

The total no.of students : 5

Student # 1 : ['20', '30', '40', '89', '78']


The sum is : 257
The percentage is 51.4 %

Student # 2 : ['85', '78', '56', '98', '75']


The sum is : 392
The percentage is 78.4 %

Student # 3 : ['36', '54', '89', '46', '89']


The sum is : 314
The percentage is 62.8 %

Student # 4 : ['93', '94', '95', '98', '99']


The sum is : 479
The percentage is 95.8 %

Student # 5 : ['98', '97', '65', '85', '45']


The sum is : 390
The percentage is 78.0 %

Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022

5. Aim : Write a Python GUI program using breezypythongui library, to demonstrate


command button events. To clear and restore a message using two command
buttons called clear and restore.

Program :

from breezypythongui import EasyFrame


class ButtonDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self)
self.label = self.addLabel(text = "Hello world!",row = 0, column = 0,columnspan
= 2,sticky = "NSEW")
self.clearBtn = self.addButton(text = "Clear",row = 1, column = 0,command =
self.clear)
self.restoreBtn = self.addButton(text = "Restore",row = 1, column = 1,state =
"disabled",command = self.restore)
def clear(self):
self.label["text"] = ""
self.clearBtn["state"] = "disabled"
self.restoreBtn["state"] = "normal"
def restore(self):
self.label["text"] = "Hello world!"
self.clearBtn["state"] = "normal"
self.restoreBtn["state"] = "disabled"
def main():
ButtonDemo().mainloop()
if __name__ == "__main__":
main()
Output :

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

7. Aim : Write a Python GUI program using breezypythongui library, to demonstrate


integer and float fields. Calculate the square root of the given number.

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):
number = self.inputField.getNumber()
result =math.sqrt(number)
self.outputField.setNumber(result)
def main():
NumberFieldDemo().mainloop()
if __name__ == "__main__":
main()
Output :

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

9. Aim : Write a Python GUI program using breezypythongui library, to demonstrate


instance variable. Implement a counter increment program on a button event, and reset
the counter to 0 when the user presses the reset button.
Program :
from breezypythongui import EasyFrame
class CounterDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, title = "Counter Demo")
self.setSize(200, 75)
self.count = 0
self.label = self.addLabel(text = "0", row = 0, column = 0, sticky = "NSEW",
columnspan= 2)
self.addButton(text = "Next", row = 1, column = 0, command = self.next)
self.addButton(text = "Reset", row = 1, column = 1, command = self.reset)
def next(self):
self.count += 1
self.label["text"] = str(self.count)
def reset(self):
self.count = 0
self.label["text"] = str(self.count)
def main():
CounterDemo().mainloop()
if __name__ == "__main__":
main()
Output :

Inference :
Week-10
Developed by : G.Sai Hemanth Kumar
Roll No. 21331A0557
Date : 13-12-2022

10. Aim : write a GUI program on multiline text are.


Program :

from breezypythongui import EasyFrame


class TextAreaDemo(EasyFrame):
def __init__(self):
EasyFrame.__init__(self, "Investment Calculator")
self.addLabel(text = "Initial amount", row = 0, column = 0)
self.addLabel(text = "Number of years", row = 1, column = 0)
self.addLabel(text = "Interest rate in %", row = 2, column = 0)
self.amount = self.addFloatField(value = 0.0, row = 0, column = 1)
self.period = self.addIntegerField(value = 0, row = 1, column = 1)
self.rate = self.addIntegerField(value = 0, row = 2, column = 1)
self.outputArea = self.addTextArea("", row = 4, column = 0,columnspan = 2,width = 50,
height = 15)
self.compute = self.addButton(text = "Compute", row = 3, column = 0,columnspan =
2,command = self.compute)
def compute(self):
startBalance = self.amount.getNumber()
rate = self.rate.getNumber() / 100
years = self.period.getNumber()
if startBalance == 0 or rate == 0 or years == 0:
return
result = "%4s%18s%10s%16s\n" % ("Year","Starting balance","Interest","Ending balance")
totalInterest = 0.0
for year in range(1, years + 1):
interest = startBalance * rate
endBalance = startBalance + interest
result += "%4d%18.2f%10.2f%16.2f\n" % \
(year, startBalance, interest, endBalance)
startBalance = endBalance
totalInterest += interest
result += "Ending balance: $%0.2f\n" % endBalance
result += "Total interest earned: $%0.2f\n" % totalInterest
self.outputArea["state"] = "normal"
self.outputArea.setText(result)
self.outputArea["state"] = "disabled"
def main():
TextAreaDemo().mainloop()
if __name__ == "__main__":
main()
Output :

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 : ____________________

You might also like