You are on page 1of 7

TYPE – C

Q 1 = Write a Python program having following functions:


(i) = A function with the following signature: Remove_letter
(sentence, letter) This function should take a string and a letter (as a
single-character string) as arguments returning a copy of that string
with every instance of the indicated letter removed for example
remove_letter ("Hello there!", "e") should return the string "Hllo
thr!". Try implementing it using <str>.split ( ) function.
(ii) =Write a function to do the following :Try implementing the
capwords () functionality using other functions, i.e , split(), capitalize
() and join( ). Compare the result with the capwords() function's
result.
( i) def remove_letter( sentence , letter ) :
new_sentence = ""
sen = sentence.split ( letter )
for i in sen :
new_sentence += i
return new_sentence

sentence = input("Enter a sentence = ")


letter = input("Enter a letter = ")
print ( remove_letter (sentence , letter) )

(i)
Enter a sentence = Hello there!
Enter a letter = e
Hllo thr!
>>>

Enter a sentence = This is Path Walla Website.


Enter a letter = Walla
This is Path Website.
>>>
Enter a sentence = We are going to do some Python code
Enter a letter = to
We are going do some Python code
>>>

(ii) def capwords( sentence ) :


sentan = [ ]
sen = sentence.split()
for i in sen :
word = ""
for j in i :
word += j.capitalize ()
sentan += [ word ]

new_sentence = " ".join(sentan)


return new_sentence

sentence = input("Enter a sentence = ")


print("Capital word = " , capwords(sentence))
print(" by upper method",sentence.upper())

Enter a sentence :- This is the Path Walla Website.


Capital word :- THIS IS THE PATH WALLA WEBSITE.
By using upper Function :- THIS IS THE PATH WALLA WEBSITE.
>>>

Enter a sentence :- We are going to do some Python code


Capital word :- WE ARE GOING TO DO SOME PYTHON CODE
By using upper Function :- WE ARE GOING TO DO SOME PYTHON CODE
>>>

Q 2 = Create a module lengthconversion.py that stores functions for


various lengths conversion
• miletokm () to convert miles to kilometers
• kmtomile() to convert kilometers to miles
• feettoinches( )
• inchestofeet()
It should also store constant values such as value of (mile in
kilometers and vice versa). [I mile = 1.609344 kilometer ; 1 feet = 12
inches] Help( ) function should display proper information.

one_mille_in_km = 1.609344
one_feet_in_inche = 12

def miletokm(x) :
"Return value in kilometer from miles "
return x * one_mille_in_km

def kmtomile(x) :
"Return value in miles from kilometer "
return x / one_mille_in_km

def feettoinches( x ):
"Return value in inches from feet "
return x / one_feet_in_inche

def inchestofeet(x) :
"Return value in feet from inches "
return x * one_feet_in_inche
Output :-
>>> import lengthconversion as l
>>> l.miletokm( 1000 )
1609.344
>>> l.miletokm( 45628 )
73431.14803200001
>>> l.kmtomile( 1000 )
621.371192237334
>>> l.kmtomile( 74873 )
46523.925276385904
>>> l.feettoinches( 1000 )
83.33333333333333
>>> l.feettoinches( 35524 )
2960.3333333333335
>>> l.inchestofeet( 1000 )
12000
>>> l.inchestofeet( 24232 )
290784
>>> help( l )
Help on module lengthconversion:

NAME
lengthconversion

FUNCTIONS
feettoinches(x)
Return value in inches from feet

inchestofeet(x)
Return value in feet from inches

kmtomile(x)
Return value in miles from kilometer

miletokm(x)
Return value in kilometer from miles

DATA
one_feet_in_inche = 12
one_mille_in_km = 1.609344

FILE
c:\users\path walla\desktop\lengthconversion.py

Q 3 = Create a module MassConversior.py that stores function for


mass conversion e.g ,
• A kgtotonne( ) to convert kg to tonnes
• A tonnetokg() to convert tonne to kg
• A kgtopound( ) to convert kg to pound
• A poundtokg()to convert pound to kg
(Also store constants 1 kg - 0.001 tonne, 1 kg = 2.20462 pound) Help( )
function should give proper information about the module.

one_kg_in_tonnes = 0.001
one_kg_in_pound = 2.20462

def kgtotonne( x ):
"Return value in tonne from kilogram"
return x * one_kg_in_tonnes

def tonnetokg( x ) :
"Return value in kilogram from tonnes"
return x / one_kg_in_tonnes
def kgtopound ( x ):
"Return value pound from kilogram"
return x * one_kg_in_pound
def poundtokg( x ) :
"Return value kilogram from pound"
return x / one_kg_in_pound

Output :-
>>> import MassConversior as m
>>> m.kgtotonne( 1000 )
1.0
>>> m.kgtotonne( 1589 )
1.589
>>> m.tonnetokg( 698743 )
698743000.0
>>> m.tonnetokg( 2000 )
2000000.0
>>> m.kgtopound ( 5634 )
12420.82908
>>> m.kgtopound ( 1000 )
2204.62
>>> m.poundtokg( 796542 )
361305.80326768337
>>> m.poundtokg( 1000 )
453.59290943563974
>>> help(m)
Help on module MassConversior:

NAME
MassConversior

FUNCTIONS
kgtopound(x)
Return value pound from kilogram

kgtotonne(x)
Return value in tonne from kilogram

poundtokg(x)
Return value kilogram from pound

tonnetokg(x)
Return value in kilogram from tonnes

DATA
one_kg_in_pound = 2.20462
one_kg_in_tonnes = 0.001

FILE
c:\users\path walla\desktop\massconversior.py

You might also like