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 title()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Iterators
Python ascii()
Python sum()
Python String casefold()
Python String index()
Python Inheritance
Python String translate()
Python ord()
Python Matrices and NumPy Arrays
Python Program to Add Two Numbers
Python Program to Make a Simple Calculator
Python String expandtabs()
Python Program to Solve Quadratic Equation
Python dict()
Machine Learning with Python for everyone - Mark E.Fenner
Deep Learning with Python - Francois Chollet
Python complex()
Python Objects and Classes
Python Set intersection_update()
Python Get Current time
Python Global, Local and Nonlocal variables
Python zip()
Python Set issuperset()
Python Program to Find LCM
Python String isalnum()
Python Program to Print Output Without a Newline
Python reversed()
Python Program to Get the Full Path of the Current Working Directory
Python File I/O Operation
Python String rjust()