In this example, we’ll learn to convert kilometers to miles and display it.
To understand this example, you should have the knowledge of the following Python programming topics:
Example: Kilometers to Miles
# Taking kilometers input from the user kilometers = float(input("Enter value in kilometers: ")) # conversion factor conv_fac = 0.621371 # calculate miles miles = kilometers * conv_fac print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))
Output
Enter value in kilometers: 3.5 3.50 kilometers is equal to 2.17 miles
Here, the user is asked to enter kilometers. This value is stored in the kilometers variable.
Since 1 kilometer is equal to 0.621371 miles, we can get the equivalent miles by multiplying kilometers with this factor.
Your turn: Modify the above program to convert miles to kilometers using the following formula and run it.
kilometers = miles / conv_fac
Related posts:
Python bytearray()
Python String translate()
Python Operators
Python String endswith()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Make a Flattened List from Nested List
Python complex()
Python dict()
Python Decorators
Python Set remove()
Python Program to Make a Simple Calculator
Python List sort()
Python String replace()
Python String isdecimal()
Python String rpartition()
Python String center()
Python String count()
Python reversed()
Python Program to Append to a File
Python Functions
Python Program to Merge Mails
Python Program to Illustrate Different Set Operations
Python Program to Catch Multiple Exceptions in One Line
Python timestamp to datetime and vice-versa
Python List extend()
Python Program to Safely Create a Nested Directory
Python Program to Check Leap Year
Python tuple()
Python Program to Find Hash of File
Python Program to Create a Long Multiline String
Python Program to Copy a File
Python Program to Print the Fibonacci sequence