0% found this document useful (0 votes)
1K views7 pages

26) Write A Program That Reads The List of Temperatures From A File Called Temps - Txt. Converts Those Temperatures To Fahrenheit, and Writes The Results To A File Called Ftemps - Txt. Sol

The document contains details about a Python programming lab course including assignments on various Python programming concepts and tasks. Some key assignments include: - Writing a program to read temperatures from a file, convert them to Fahrenheit, and write the results to another file. - Creating a Product class with methods to calculate product prices based on discount levels for different purchase quantities. - Developing a Time class to convert time in seconds to minutes and hours format. - Building a Converter class to convert between different length units like inches, feet, meters etc. - Implementing a power class to calculate x to the power of n. - Reversing words in a string. - Creating a

Uploaded by

Rama Priya
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)
1K views7 pages

26) Write A Program That Reads The List of Temperatures From A File Called Temps - Txt. Converts Those Temperatures To Fahrenheit, and Writes The Results To A File Called Ftemps - Txt. Sol

The document contains details about a Python programming lab course including assignments on various Python programming concepts and tasks. Some key assignments include: - Writing a program to read temperatures from a file, convert them to Fahrenheit, and write the results to another file. - Creating a Product class with methods to calculate product prices based on discount levels for different purchase quantities. - Developing a Time class to convert time in seconds to minutes and hours format. - Building a Converter class to convert between different length units like inches, feet, meters etc. - Implementing a power class to calculate x to the power of n. - Reversing words in a string. - Creating a

Uploaded by

Rama Priya
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/ 7

DEPARTMENT OF COMPUTER SCIENCE &ENGINEERING

PYTHON PROGRAMMING LAB L T P C


II Year – I Semester 0 0 3 1.5

26) Write a program that reads the list of temperatures from a file called temps.txt.
converts those temperatures to Fahrenheit, and writes the results to a file called
ftemps.txt.

Sol:

file1=open('ftemps.txt','w')
temp=[int(line.strip()) for line in open('temps.txt','r')]
for t in temp:
print(t*9/5+32,file=file1)
file1.close()

data in temps.txt
98
99
100
102
output:

data in ftemps.txt

208.4
210.2
212.0
215.6

27) Write a class called Product . The class should have fields called name, amount, and price,
holding the product’s name, the number of items of that product in stock , and the regular price
of the product. There should be a method get_price thatrecieves the number of items to be
bought and returns the cost of the buying that many items, where the regular price is charged for
orders of less than 10 items , a 10% discount is applied for orders of between 10 and 99 items,
and a 20% discount is applied for orders of 100 or more items. There should also be a method
called make_purchase that recives the number of items to be bought and decreases amount by
that much.

PROGRAM:

class Product:

def __init__(self,amount,price):
self.amount=amount
self.price=price

def get_price(self):
while self.amount>=0:
if self.amount<10:
return self.amount*self.price
elif 10<=self.amount<99:
return 0.9*self.amount*self.price
else:
return 0.8*self.amount*self.price

def make_purchase(self):
return int(input('Number of elements to buy'))-self.amount

costs=Product(int(input('Digit amount of items\n')),int(input('Digit price of items\n')))


purchases=Product(int(input('Digit amount of items to buy\n')),int(input('Digit price of items\
n')))
print('Final costs = ',costs.get_price())
print('Elements to buy=',purchases.make_purchase())

OUTPUT:

Digit amount of items


100
Digit price of items
5
Digit amount of items to buy
3
Digit price of items
2
Final costs = 400.0
Number of elements to buy5
Elements to buy= 2

28) Write a class called Time whose only filed is a time in seconds. It should have a method called
convert_to_minutes that returns the string of the minutes and seconds formatted as in the
following example: if seconds is 230 , the method should return ‘5:50’. It should also have a
method called convert_to_hours that returns the string of hours, minutes and seconds
formatted analogously to the previous method.

Program:
class Time:
def __init__(self,sec):
self.sec=sec
def convert_to_minutes(self):
n=self.sec
minutes=n//60
seconds=n%60
return(str(minutes)+":"+str(seconds))
def convert_to_hours(self):
n=self.sec
hours=n//3600
minutes=(n//60)%60
seconds=n%60
return(str(hours)+":"+str(minutes)+":"+str(seconds))
a=int(input("Enter seconds: "))
c=Time(a)
print("Time in minutes:seconds format --->",c.convert_to_minutes())
print("Time in hours:minutes:seconds format --->",c.convert_to_hours())

29) Write a class called Converter. This user will pass a length and a unit when declaring an object from
the class –for example, c=Converter(9,’inches’). The possible units are inches , feet ,yards , miles,
kilometers , meters, centimeters , and millimeters . For each of this units there should be a method that
returns the length covered into those units. For example ,using the Converter object created above ,
the user could call c.feet() and shout get 0.75 as the result.

Sol:
class Converter:
def __init__(self,length,unit):
self.length=length
self.unit=unit
def feet(self):
if(self.unit=='feet'):
return self.length
elif(self.unit=='inches'):
return self.length/12
elif(self.unit=='yards'):
return self.length/0.333
elif(self.unit=='miles'):
return self.length/0.000189
elif(self.unit=='millimeters'):
return self.length/304.8
elif(self.unit=='centimeters'):
return self.length/30.48
elif(self.unit=='meters'):
return self.length/0.305
elif(self.unit=='kilometers'):
return self.length/0.000305
def inches(self):
if(self.unit=='feet'):
return self.length*12
elif(self.unit=='inches'):
return self.length
elif(self.unit=='yards'):
return self.length*36
elif(self.unit=='miles'):
return self.length*63360
elif(self.unit=='millimeters'):
return self.length*0.0393701
elif(self.unit=='centimeters'):
return self.length*0.393701
elif(self.unit=='meters'):
return self.length*39.3701
elif(self.unit=='kilometers'):
return self.length*39370.1
def yards(self):
if(self.unit=='feet'):
return self.length*0.333333
elif(self.unit=='inches'):
return self.length*0.0277778
elif(self.unit=='yards'):
return self.length
elif(self.unit=='miles'):
return self.length*1760
elif(self.unit=='millimeters'):
return self.length*0.00109361
elif(self.unit=='centimeters'):
return self.length*0.0109361
elif(self.unit=='meters'):
return self.length*1.09361
elif(self.unit=='kilometers'):
return self.length*1093.61

def miles(self):
if(self.unit=='feet'):
return self.length*0.000189394
elif(self.unit=='inches'):
return self.length*63360
elif(self.unit=='yards'):
return self.length*0.027777728
elif(self.unit=='miles'):
return self.length
elif(self.unit=='millimeters'):
return self.length/1609344
elif(self.unit=='centimeters'):
return self.length/160934.4
elif(self.unit=='meters'):
return self.length/1609.344
elif(self.unit=='kilometers'):
return self.length/1.609
def kilometers(self):
if(self.unit=='feet'):
return self.length/3280.84
elif(self.unit=='inches'):
return self.length/39370.1
elif(self.unit=='yards'):
return self.length/1093.61
elif(self.unit=='miles'):
return self.length/0.621371
elif(self.unit=='millimeters'):
return self.length/1000000
elif(self.unit=='centimeters'):
return self.length/100000
elif(self.unit=='meters'):
return self.length/1000
elif(self.unit=='kilometers'):
return self.length
def meters(self):
if(self.unit=='feet'):
return self.length/3.28084
elif(self.unit=='inches'):
return self.length/39.3701
elif(self.unit=='yards'):
return self.length/1.09361
elif(self.unit=='miles'):
return self.length/0.000621371
elif(self.unit=='millimeters'):
return self.length/1000
elif(self.unit=='centimeters'):
return self.length/100
elif(self.unit=='meters'):
return self.length
elif(self.unit=='kilometers'):
return self.length/0.001
def centimeters(self):
if(self.unit=='feet'):
return self.length/0.0328084
elif(self.unit=='inches'):
return self.length/0.393701
elif(self.unit=='yards'):
return self.length/0.0109361
elif(self.unit=='miles'):
return self.length*160934
elif(self.unit=='millimeters'):
return self.length/10
elif(self.unit=='centimeters'):
return self.length
elif(self.unit=='meters'):
return self.length*100
elif(self.unit=='kilometers'):
return self.length*100000
def millimeters(self):
if(self.unit=='feet'):
return self.length*304.8
elif(self.unit=='inches'):
return self.length/0.0393701
elif(self.unit=='yards'):
return self.length/0.00109361
elif(self.unit=='miles'):
return self.length*1609340
elif(self.unit=='millimeters'):
return self.length
elif(self.unit=='centimeters'):
return self.length*10
elif(self.unit=='meters'):
return self.length*100
elif(self.unit=='kilometers'):
return self.length*1000000

len=int(input("Enter length: "))


type=input("Enter unit type:
inches,feet,yards,miles,millimeters,centimeters,meters,kilometers---> ")
c=Converter(len,type)
print("Length in Feet: ",round(c.feet(),3))
print("Length in Inches: ",round(c.inches(),3))
print("Length in Yards: ",round(c.yards(),3))
print("Length in Miles: ",round(c.miles(),3))
print("Length in Kilometers: ",round(c.kilometers(),3))
print("Length in Meters: ",round(c.meters(),3))
print("Length in Centimeters: ",round(c.centimeters(),3))
print("Length in Millimeters: ",round(c.millimeters(),3))

output:
Enter length: 6
Enter unit type: inches,feet,yards,miles,millimeters,centimeters,meters,kilometers---> inches
Length in Feet: 0.5
Length in Inches: 6
Length in Yards: 0.167
Length in Miles: 380160
Length in Kilometers: 0.0
Length in Meters: 0.152
Length in Centimeters: 15.24
Length in Millimeters: 152.4

30) Write a Python class to implement pow(x,n).

PROGRAM:

class power:
def pow(self,x,n):
print("pow(",x,",",n,") =",x**n)
p=power()
x=int(input("Enter 'x' value : "))
n=int(input("Enter 'n' value : "))
p.pow(x,n)

OUTPUT:

Enter 'x' value : 4


Enter 'n' value : 2
pow( 4 , 2 ) = 16

31) Write a Python class to reverse the a string word by word.

PROGRAM:

class py_solution:
def reverse_words(self, s):
return ' '.join(reversed(s.split()))

print(py_solution().reverse_words(Welcome sri'))

OUTPUT:

sri Welcome

32) Write a Python program that opens a file dialog that allows you to select a
text file. The program then displays the contents of the file in a textbox.

Sol:
from tkinter import filedialog
from tkinter import Tk
from tkinter import *
root = Tk()
root.fileName = filedialog.askopenfilename(filetypes=(("Text Files",".txt"),("All Files","*.*")))
text1 = open(root.fileName).read()
T = Text(root, height=25, width=80)
T.pack()
T.insert(END,text1) #END (or “end”) corresponds to the position just after the last character in the buffer.
root.mainloop()

OUTPUT:

33) Write a Python program to demonstrate Try/expect/else.

PROGRAM:
# program to print the reciprocal of even numbers

try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)

OUTPUT:
Enter a number: 25
Not an even number!

34) Write a Python program to demonstrate try/finally and with/as .

PROGRAM:

# Python program to demonstrate finally

try:
k = 5//1 # No exception raised
print(k)

# intends to handle zerodivision exception


except ZeroDivisionError:
print("Can't divide by zero")

finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')

OUTPUT:
5
This is always executed

You might also like