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 String rjust()
Python Set clear()
Python help()
Python Recursion
Python frozenset()
Python timestamp to datetime and vice-versa
Python filter()
Python globals()
Python Program to Solve Quadratic Equation
Python Objects and Classes
Python Program to Compute the Power of a Number
Python @property decorator
Python String partition()
Python Program to Print all Prime Numbers in an Interval
Python *args and **kwargs
Python print()
Python Program to Find the Factorial of a Number
Python String isalpha()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Find ASCII Value of Character
Python eval()
Python String isprintable()
Python Dictionary values()
Python Program to Print Hello world!
Python list()
Python Program to Differentiate Between del, remove, and pop on a List
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String expandtabs()
Python String count()
Python Program to Safely Create a Nested Directory
Python frozenset()
Python bytearray()