Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

PYTHON PROGRAMMING LAB

INDEX
Page Marks
S.No Date Name of the Experiment Remarks
No Awarded
Write a program that asks the user for a
1. weight in kilograms and converts it to
pounds.
There are 2.2 pounds in a kilogram.
Write a program that asks the user to enter
three numbers (use three separate input
statements). Create variables called total
2.
and average that hold the sum and average
of the
three numbers and print out the values of
total and average.
Write a program that uses a for loop to print
3.
the numbers 8, 11, 14, 17, 20, . . . , 83, 86,
89.
Write a program that asks the user for their
4. name and how many times to print it. The
program should print out the user’s name
the specified number of times.
Use a for loop to print a triangle like the one
5.
below. Allow the user to specify how high
the triangle should be.
Generate a random number between 1 and
10. Ask the user to guess the number and
6.
print
a message based on whether they get it
right or not.
Write a program that asks the user for two
numbers and prints Close if the numbers
7.
are
within .001 of each other and Not close
otherwise.
Write a program that asks the user to enter
8.
a word and prints out whether that word
contains any vowels.
Write a program that asks the user to enter
two strings of the same length. The
program
should then check to see if the strings are
of the same length. If they are not, the
9. program
should print an appropriate message and
exit. If they are of the same length, the
program
should alternate the characters of the two
strings. For example, if the user enters
abcde and
ABCDE the program should print out
AaBbCcDdEe.
Write a program that asks the user for a
large integer and inserts commas into it
according
10
to the standard American convention for
.
commas in large numbers. For instance, if
the
user enters 1000000, the output should be
1,000,000.
In algebraic expressions, the symbol for
multiplication is often left out, as in 3x+4y or
3(x+5). Computers prefer those
expressions to include the multiplication
11
symbol, like
.
3*x+4*y or 3*(x+5). Write a program that
asks the user for an algebraic expression
and
then inserts multiplication symbols where
appropriate.
Write a program that generates a list of 20
random numbers between 1 and 100.
(a) Print the list.
(b) Print the average of the elements in the
12 list.
. (c) Print the largest and smallest values in
the list.
(d) Print the second largest and second
smallest entries in the list
(e) Print how many even numbers are in
the list.
Write a program that asks the user for an
13
integer and creates a list that consists of
.
the
factors of that integer.
Write a program that generates 100
random integers that are either 0 or 1. Then
14 find the
. longest run of zeros, the largest number of
zeros in a row. For instance, the longest
run of
zeros in [1,0,1,1,0,0,0,0,1,0,0] is 4.
Write a program that removes any repeated
15 items from a list so that each item appears
. at
most once. For instance, the list
[1,1,2,3,4,3,0,0] would become [1,2,3,4,0].
16 Write a program that asks the user to enter
. a length in feet. The program should then
give
the user the option to convert from feet into
inches, yards, miles, millimeters,
centimeters,
meters, or kilometers. Say if the user enters
a 1, then the program converts to inches, if
they enter a 2, then the program converts
to yards, etc. While this can be done with if
statements,it is much shorter with lists and
it is also easier to add new conversions if
you
use lists.
Write a function called sum_digits that is
17
given an integer num and returns the sum
.
of the
digits of num.
Write a function called first_diff that is given
18
two strings and returns the first location in
.
which the strings differ. If the strings are
identical, it should return -1.
19 Write a function called number_of_factors
. that takes an integer and returns how many
factors the number has.
Write a function called is_sorted that is
20
given a list and returns True if the list is
.
sorted and
False otherwise.
Write a function called root that is given a
21 number x and an integer n and returns x 1/
. n . In
the function definition, set the default value
of n to 2.
22 Write a function called primes that is given
. a number n and returns a list of the first n
primes. Let the default value of n be 100.
Write a function called merge that takes
two already sorted lists of possibly different
23
lengths, and merges them into a single
.
sorted list.
(a) Do this using the sort method. (b) Do
this without using the sort method.
Write a program that asks the user for a
word and finds all the smaller words that
can be
24
made from the letters of that word. The
.
number of occurrences of a letter in a
smaller word
can’t exceed the number of occurrences of
the letter in the user’s word.
25 Write a program that reads a file consisting
. of email addresses, each on its own line.
Your
program should print out a string consisting
of those email addresses separated by
semicolons.
Write a program that reads a list of
26 temperatures from a file called temps.txt,
. converts
those temperatures to Fahrenheit, and
writes the results to a file called ftemps.txt.
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 that receives the number
of items
to be bought and returns a the cost of
27 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 receives the number
of items to be
bought and decreases amount by that
much.
Write a class called Time whose only field
is a time in seconds. It should have a
method
called convert_to_minutes that returns a
string of minutes and seconds formatted as
in the
28
following example: if seconds is 230, the
.
method should return '5:50'. It should also
have a
method called convert_to_hours that
returns a string of hours, minutes, and
seconds
formatted analogously to the previous
method.
29 Write a class called Converter. The 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
these units there should be a method that
returns the length converted into those
units. For
example, using the Converter object
created above, the user could call c.feet()
and should
get 0.75 as the result.
30
Write a Python class to implement pow(x,
.
n).
31
Write a Python class to reverse a string
.
word by word.
Write a program that opens a file dialog
32 that allows you to select a text file. The
. program
then displays the contents of the file in a
textbox.
33
Write a program to demonstrate Try/except/
.
else.
34
Write a program to demonstrate
.
try/finally and with/as.
DATE:-

1) Write a program that asks the user for a weight in kilograms and converts it to pounds.
There are 2.2 pounds in a kilogram.

PROGRAM:-

kg=float(input("Enter the weight n kilograms to convert into pounds: "))


pounds=kg*2.2046
print(kg,'kg=',pounds,'Pounds')

OUTPUT:-

Enter the weight n kilograms to convert into pounds: 45


45.0 kg= 99.20700000000001 Pounds

1
DATE:-

2) Write a program that asks the user to enter three numbers (use three separate input
statements). Create variables called total and average that hold the sum and average of the
three numbers and print out the values of total and average.

PROGRAM:-

print("Input three numbers")


a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
c=int(input("Enter the third number: "))
sum=a+b+c
average=sum/3
print("The sum is: ",sum)
print("The average is: ",average)

OUTPUT:-

Input three numbers


Enter the first number: 45
Enter the second number: 12
Enter the third number: 9
The sum is: 66
The average is: 22.0

2
DATE:-

3) Write a program that uses a for loop to print the numbers 8, 11, 14, 17, 20, . . . , 83, 86, 89.

PROGRAM:-

for i in range(8,90,3):
print(i,end=" ")

OUTPUT:-

8 11 14 17 20 23 26 29 32 35 38 41 44 47 50 53 56 59 62 65 68 71 74 77 80 83 86 89

3
DATE:-

4) Write a program that asks the user for their name and how many times to print it. The
program should print out the user’s name the specified number of times.

PROGRAM:-

name=input("Enter the to be printed: ")


x=int(input("Number of times to be printed: "))
for i in range(x):
print(name)

OUTPUT:-

Enter the to be printed: PYTHON


Number of times to be printed: 5
PYTHON
PYTHON
PYTHON
PYTHON
PYTHON

4
DATE:-

5) Use a for loop to print a triangle like the one below. Allow the user to specify how high the
triangle should be.
*
**
***
****
PROGRAM:-

n=int(input("Enter number of times: "))


for i in range(0,n):
for j in range(0,i+1):
print('*',end=" ")
print("\r")

OUTPUT:-

Enter number of times: 5


*
**
***
****
*****

5
DATE:-

6) Generate a random number between 1 and 10. Ask the user to guess the number and print
a message based on whether they get it right or not.

PROGRAM:-

import random
n=random.randint(1,10)
x=int(input("Enter your guess: "))
if x==n:
print("Correct guess")
else:
print("Wrong guess")

OUTPUT:-

Enter your guess: 4


Wrong guess

6
DATE:-

7) Write a program that asks the user for two numbers and prints Close if the numbers are
within .001 of each other and Not close otherwise.

PROGRAM:-

a=float(input("Enter the first number: "))


b=float(input("Enter the second number: "))
if abs(a-b)<=0.001:
print("Close")
else:
print("Not close")

OUTPUT:-

Enter the first number: 45.1


Enter the second number: 45.00003
Not close

7
DATE:-

8) Write a program that asks the user to enter a word and prints out whether that word
contains any vowels.

PROGRAM:-

x=input("Enter a word: ")


y=['a','e','i','o','u','A','E','I','O','U']
flag=0
for i in y:
if i in x:
flag=1
break
if flag==1:
print("Word contains vowel")
else:
print("Word does not contain vowel")

OUTPUT:-

Enter a word: PYTHON


Word contains vowel

8
DATE:-

9) Write a program that asks the user to enter two strings of the same length. The program
should then check to see if the strings are of the same length. If they are not, the program
should print an appropriate message and exit. If they are of the same length, the program
should alternate the characters of the two strings. For example, if the user enters abcde and
ABCDE the program should print out AaBbCcDdEe.

PROGRAM:-

a=input("Enter the first string: ")


b=input("Enter the second string: ")
result=' '
if(len(a)!=len(b)):
print("Strings are not of same length")
else:
for x,y in zip(a,b):
result=result+x+y
print(result)

OUTPUT:-

Enter the first string: python


Enter the second string: PYTHON
pPyYtThHoOnN

9
DATE:-

10) Write a program that asks the user for a large integer and inserts commas into it
according to the standard American convention for commas in large numbers. For instance, if
the user enters 1000000, the output should be 1,000,000.

PROGRAM:-

num=int(input("Enter a long integer: "))


def place_value(num):
return("{:,}".format(num))
print(place_value(num))

OUTPUT:-

Enter a long integer: 54122841251


54,122,841,251

10
DATE:-

11) In algebraic expressions, the symbol for multiplication is often left out, as in 3x+4y or
3(x+5). Computers prefer those expressions to include the multiplication symbol, like 3*x+4*y
or 3*(x+5). Write a program that asks the user for an algebraic expression and then inserts
multiplication symbols where appropriate.

PROGRAM:-

s=input("Enter algebraic expression: ")


l=list(s)
result=''
i=0
while(i<len(l)):
if l[i]=='(':
index=l.index(')')
s2=''.join(l[i:index+1])
result=result+'*'+s2
i=i+len(s2)
elif l[i].isalpha():
result=result+'*'+l[i]
i=i+1
else:
result=result+l[i]
i=i+1
print(result)

OUTPUT:-

Enter algebraic expression: 3x+4y


3*x+4*y

11
DATE:-

12) Write a program that generates a list of 20 random numbers between 1 and 100. (a) Print
the list. (b) Print the average of the elements in the list. (c) Print the largest and smallest
values in the list. (d) Print the second largest and second smallest entries in the list (e) Print
how many even numbers are in the list.

PROGRAM:-

import random
l=[]
for i in range(20):
l.append(random.randint(1,100))
print("List: ",l)
print("Average: ",round(sum(l)/len(l),2))
print("Largest in the list: ",max(l))
print("Smallest in the list: ",min(l))
l1=sorted(l)
print("Second smallest in the list: ",l1[-2])
print("Second largest in the list: ",l1[1])
count=0
for i in l:
if i%2==0:
count+=1
print("Number of even numbers in the list: ",count)

OUTPUT:-

List: [47, 80, 51, 13, 3, 64, 36, 83, 31, 28, 17, 62, 61, 41, 58, 76, 11, 16, 71, 96]
Average: 47.25
Largest in the list: 96
Smallest in the list: 3
Second smallest in the list: 83
Second largest in the list: 11

12
DATE:-

13) Write a program that asks the user for an integer and creates a list that consists of the
factors of that integer.

PROGRAM:-

n=int(input("Enter a number: "))


l= [ ]
for i in range(1,n+1):
if(n%i==0):
l.append(i)
print(l)

OUTPUT:-

Enter a number: 102


[1, 2, 3, 6, 17, 34, 51, 102]

13
DATE:-

14) Write a program that generates 100 random integers that are either 0 or 1. Then find the
longest run of zeros, the largest number of zeros in a row. For instance, the longest run of
zeros in [1,0,1,1,0,0,0,0,1,0,0] is 4.

PROGRAM:-

import random
x=[ ]
for i in range(100):
x.append(random.randint(0,1))
maxzero=0
count=0
for i in range(len(x)):
if(x[i]==0):
count=count+1
if(i==len(x)-1):
if(count>maxzero):
maxzero=count
count=0
print("Longest run of zeroes in a row is: ",maxzero)

OUTPUT:-

Longest run of zeroes in a row is: 0

14
DATE:-

15) Write a program that removes any repeated items from a list so that each item appears at
most once. For instance, the list [1,1,2,3,4,3,0,0] would become [1,2,3,4,0].

PROGRAM:-

l=list(map(int,input("Enter the elements into the list with duplication: ").split(',')))


s=[]
for i in l:
if i not in s:
s.append(i)
print(s)

OUTPUT:-

Enter the elements into the list with duplication: 1,2,5,45,85,48,2,45


[1, 2, 5, 45, 85, 48]

15
DATE:-

16) Write a program that asks the user to enter a length in feet. The program should then give
the user the option to convert from feet into inches, yards, miles, millimeter, centimeter,
meters, or kilometer. Say if the user enters a 1, then the program converts to inches, if they
enter a 2, then the program converts to yards, etc. While this can be done with if statements, it
is much shorter with lists and it is also easier to add new conversions if you use lists.

PROGRAM:-

feet=int(input("Enter feet: "))


ch=int(input("enter choice 1: inches 2:yards 3 miles 4:millimeters 5:centimeter 6:Meters
7:kilometer "))
if(ch==1):
print(round(feet *12.3))
elif(ch==2):
print(round(feet *0.333,3))
elif(ch==3):
print(round( feet *0.000189,3))
elif(ch==4):
print(round(feet *304.8,3))
elif(ch==5):
print(round(feet *30.46,3))
elif(ch==6):
print(round(feet *0.305,3))
else:
print(round(feet *0.000305,3))

OUTPUT:-

Enter feet: 29
enter choice 1: inches 2:yards 3 miles 4:millimeters 5:centimeter 6:Meters 7:kilometer 4
8839.2

16
DATE:-

17) Write a function called sum_digits that is given an integer num and returns the sum of the
digits of num.

PROGRAM:-

def sum_digits(num):
sum=0
while(num>0):
sum=sum+num%10
num=num//10
return sum
x=int(input("Enter a number: "))
s=sum_digits(x)
print("Sum of digits: ",s)

OUTPUT:-

Enter a number: 97894


Sum of digits: 37

17
DATE:-

18) Write a function called first_diff that is given two strings and returns the first location in
which the strings differ. If the strings are identical, it should return -1.

PROGRAM:-

def first_diff(s1,s2):
if(s1==s2):
return -1
else:
if len(s1)==len(s2):
for i in range (len(s1)):
if s1[i]!=s2[i]:
return (i+1)
s1=input("Enter string 1: ")
s2 = input("enter string 2: ")
x=first_diff (s1,s2)
if (x ==-1):
print (" strings are identical")
else:
print ("First difference occurs at index:",x)

OUTPUT:-

Enter string 1: APPLY


enter string 2: APPLE
First difference occurs at index: 5

18
DATE:-

19) Write a function called number_of_factors that takes an integer and returns how many
factors the number has.

PROGRAM:-

def number_of_factors(n):
fact_count=0
for i in range(1,n+1):
if(n%i==0):
fact_count+=1
return fact_count
n=int(input("Enter an integer: "))
x=number_of_factors(n)
print("factors count is ",x)

OUTPUT:-

Enter an integer: 45
factors count is 6

19
DATE:-

20) Write a function called is_sorted that is given a list and returns True if the list is sorted
and False otherwise.

PROGRAM:-

def is_sorted(l):
x=l[:]
x.sort()
if l==x:
return True
else:
return False
l=list(input("Enter list items: ").split())
print(is_sorted(l))

OUTPUT:-

Enter list items: 45 85 1 21 65 12 9


False

20
DATE:-

21) Write a function called root that is given a number x and an integer n and returns x1/n. In
the function definition, set the default value of n to 2.

PROGRAM:-

def root(x,n=2):
return(x**(1/n))
x=int(input("Enter 'x' value: "))
n=int(input("Enter 'n' value: "))
a=root(x,n)
b=root(x)
print("Root value with n value: ",a)
print("Root value with out n value(Default 2): ",b)

OUTPUT:-

Enter 'x' value: 24


Enter 'n' value: 5
Root value with n value: 1.888175022589804
Root value with out n value(Default 2): 4.898979485566356

21
DATE:-

22) Write a function called primes that is given a number n and returns a list of the first n
primes. Let the default value of n be 100.

PROGRAM:-

def isprime(n):
if(n<2):
return False
for i in range(2,n//2+1):
if(n%i==0):
return False
return True
a=int(input("Enter lower range: "))
b=int(input("Enter higher range: "))
for x in range(a,b):
if isprime(x):
print(x,end=' ')

OUTPUT:-

Enter lower range: 0


Enter higher range: 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

22
DATE:-

23) Write a function called merge that takes two already sorted lists of possibly different
lengths, and merges them into a single sorted list. (a) Do this using the sort method. (b) Do this
without using the sort method.

PROGRAM:-

def merge(s1,s2):
s=s1+s2
s.sort()
return s
a=list(map(int,input("Enter sorted list1: ").split()))
b=list(map(int,input("Enter sorted list2: ").split()))
x=merge(a,b)
print("Sorted list after merging: ",x)

OUTPUT:-

Enter sorted list1: 1 2 5 87 65


Enter sorted list2: 45 85 47 1 2 5 8 46
Sorted list after merging: [1, 1, 2, 2, 5, 5, 8, 45, 46, 47, 65, 85, 87]

23
DATE:-

24) Write a program that asks the user for a word and finds all the smaller words that can be
made from the letters of that word. The number of occurrences of a letter in a smaller word
can’t exceed the number of occurrences of the letter in the user’s word.

PROGRAM:-

from itertools import permutations


s=input("Enter a word: ")
for i in range(2,len(s)):
for p in permutations(s,i):
print(''.join(p))

OUTPUT:-

Enter a word: CSE


CS
CE
SC
SE
EC
ES

24
DATE:-

25) Write a program that reads a file consisting of email addresses, each on its own line. Your
program should print out a string consisting of those email addresses separated by
semicolons.

PROGRAM:-

file=open(input("Enter file name: "))


Lines=file.readlines()
for line in range(len(Lines)):
if(line==len(Lines)-1):
print('{}'.format(Lines[line].strip()))
else:
print('{}'.format(Lines[line].strip()),end=";")

OUTPUT:-

Enter file name: PYTHON 1.txt


PYTHON PROGRAMMING LAB;PYTHON PROGRAM;PROGRAM

25
DATE:-

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

PROGRAM:-

file1=open('temps.txt','r')
lines=file1.readlines()
file2=open('ftemps.txt','w')
for i in range(len(lines)):
c=lines[i].strip()
x=float(c)
y=x*1.8
f=round(y+32,2)
file2.write(str(f)+'\n')
file2.close()

OUTPUT:-

26
DATE:-

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 that receives the number of items to
be bought and returns a the cost of 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 receives the number of items to be bought and
decreases amount by that much

PROGRAM:-

class product:
def __init__(self,name,items,price):
self.name=name
self.items=items
self.price=price
def getprice(self,n):
if n<10:
print("Regular price is charged for your orders ")
cost=n*self.price
print("If you place above 9 items you get 10% discount")
print("If you place above 99 items you get 20% discount")
elif n>10 and n<100:
print("10% discounts is applied for your orders")
cost=n*self.price
discount=(cost*10)/100
finalcost=cost-discount
print("Actual cost: ",cost)
print("10% discount: ",finalcost)
print("Cost after 10% discount: ",discount)
print("If you place above 99 items you get 20% discount")
else:
print("20% discouunt is applied for your order")
cost=n*self.price
discount=(cost*20)/100
finalcost=cost-discount
print("Actual cost: ",cost)
print("20% discount: ",discount)
print("Cost after 20% discount: ",finalcost)
def my_purchase(self,n):
if n<10:
print("Regular price is charged for your orders")
cost=n*self.price
print("Final cost: ",cost)
elif (n>10) and (n<100):

27
print("10% discount is applied for your order")
cost=n*self.price
discount=(cost*10)/100
finalcost=cost-discount
print("Actual cost: ",cost)
print("10% discount: ",discount)
print("Final cost after 10% discount: ",finalcost)
else:
print("20% discouunt is applied for your order")
cost=n*self.price
discount=(cost*20)/100
finalcost=cost-discount
print("Actual cost: ",cost)
print("20% discount: ",discount)
print("Final cost after 20% discount: ",finalcost)
p=product("PEN",200,5)
n=int(input("Enter number of pens you want to buy: "))
p.getprice(n)
n=int(input("Enter number of pens you want to buy: "))
p.my_purchase(n)

OUTPUT:-

Enter number of pens you want to buy: 15


10% discounts is applied for your orders
Actual cost: 75
10% discount: 67.5
Cost after 10% discount: 7.5
If you place above 99 items you get 20% discount
Enter number of pens you want to buy: 101
20% discouunt is applied for your order
Actual cost: 505
20% discount: 101.0
Final cost after 20% discount: 404.0

28
DATE:-

28) Write a class called Time whose only field is a time in seconds. It should have a method
called convert_to_minutes that returns a string of 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 a 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())

OUTPUT:-

Enter seconds: 324


Time in minutes seconds format----> 5:24
Time in hours minutes seconds format----> 0:5:24

29
DATE:-

29) Write a class called Converter. The 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 these units
there should be a method that returns the length converted into those units. For example,
using the Converter object created above, the user could call c.feet() and should get 0.75 as the
result.

PROGRAM:-

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

30
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

31
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

32
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: 8
Enter unit type: inches,feet,yards,miles,millimeters,centimeters,meters,kilometers---->feet
Length in feet: 8
Length in Inches: 96
Length in Yards: 2.667
Length in Miles: 0.002
Length in Kilometers: 0.002
Length in Meters: 2.438
Length in Centimeters: 243.84
Length in Millimeters: 0.026

33
DATE:-

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: 5
pow( 4 , 5 )= 1024

34
DATE:-

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

PROGRAM:-

class reverse:
def rev_sentence(self,sentence):
words=sentence.split(' ')
reverse_sentence=' '.join(reversed(words))
print(reverse_sentence)
c=reverse()
c.rev_sentence(input("Enter the string: "))

OUTPUT:-

Enter the string: WELCOME TO THE WORLD OF PROGRAMMING


PROGRAMMING OF WORLD THE TO WELCOME

35
DATE:-

32) Write a 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.

PROGRAM:-

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)
root.mainloop()

OUTPUT:-

36
DATE:-

33) Write a program to demonstrate Try/except/else.

PROGRAM:-

try:
a=int(input("Enter 'a' value: "))
b=int(input("enter 'b' value: "))
c=a//b
except ZeroDivisionError:
print("Division can't possible(b=0)")
else:
print("a//b Value: ",c)

OUTPUT:-

Enter 'a' value: 42


enter 'b' value: 5
a//b Value: 8

37
DATE:-

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

PROGRAM:-

try:
a=int(input("Enter 'a' value: "))
b=int(input("Enter 'b' value: "))
c=a//b
except ZeroDivisionError:
print("Division can't possible(b=0)")
else:
print("a//b value: ",c)
finally:
print("End of the program")

OUTPUT:-

Enter 'a' value: 49


Enter 'b' value: 5
a//b value: 9
End of the program

38

You might also like