Uploaded by Sanika Sree A.K

Python assingment

advertisement
Write a Python program to convert the
distance (in feet) to inches, yards, and miles.
Unit Equivalents
Conversion Factors
(longer to shorter units of
measurement)
Conversion Factors
(shorter to longer units of measurement)
1 foot = 12 inches
12 inches
_______
1 foot
1 foot
_______
12 inches
1 yard = 3 feet
3 feet
_______
1 yard
1 yard
_______
3 feet
1 mile = 5,280 feet
5,280 feet
________
1 mile
1 mile
________
5,280 feet
Pictorial Presentation:
Sample Solution:
Python Code :
d_ft = int(input("Input distance in feet: "))
d_inches = d_ft * 12
d_yards = d_ft / 3.0
d_miles = d_ft / 5280.0
print("The distance in inches is %i inches." % d_inches)
print("The distance in yards is %.2f yards." % d_yards)
print("The distance in miles is %.2f miles." % d_miles)
Sample Output:
Input distance in feet: 100
The distance in inches is 1200 inches.
The distance in yards is 33.33 yards.
The distance in miles is 0.02 miles.
Write a Python program to change the
position of every n-th value with the (n+1)th
in a list.
Sample Solution:Python Code:
from itertools import zip_longest, chain, tee
def replace2copy(lst):
lst1, lst2 = tee(iter(lst), 2)
return list(chain.from_iterable(zip_longest(lst[1::2], lst[::2])))
n = [0,1,2,3,4,5]
print(replace2copy(n))
Sample Output:
[1, 0, 3, 2, 5, 4]
Flowchart:
Write a
Python
program to convert temperatures to and from
celsius, fahrenheit.
Python: Centigrade and Fahrenheit Temperatures :
The centigrade scale, which is also called the Celsius scale, was developed by Swedish
astronomer Andres Celsius. In the centigrade scale, water freezes at 0 degrees and boils
at 100 degrees. The centigrade to Fahrenheit conversion formula is:
Fahrenheit and centigrade are two temperature scales in use today. The Fahrenheit scale
was developed by the German physicist Daniel Gabriel Fahrenheit . In the Fahrenheit
scale, water freezes at 32 degrees and boils at 212 degrees.
C = (5/9) * (F - 32)
where F is the Fahrenheit temperature. You can also use this Web page to convert
Fahrenheit temperatures to centigrade. Just enter a Fahrenheit temperature in the text box
below, then click on the Convert button.
Sample Solution:-
Python Code:
temp = input("Input the
etc.) : ")
temperature you like to convert? (e.g., 45F, 102C
degree = int(temp[:-1])
i_convention = temp[-1]
if i_convention.upper() == "C":
result = int(round((9 * degree) / 5 + 32))
o_convention = "Fahrenheit"
elif i_convention.upper() == "F":
result = int(round((degree - 32) * 5 / 9))
o_convention = "Celsius"
else:
print("Input proper convention.")
quit()
print("The temperature in", o_convention, "is", result, "degrees.")
Copy
Sample Output:
Input the temperature you like to convert? (e.g., 45F, 102C etc.) :
104f
The temperature in Celsius is 40 degrees.
Flowchart:
Download