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 List extend()
Python break and continue
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Sort a Dictionary by Value
Python Program to Delete an Element From a Dictionary
Python Operator Overloading
Python @property decorator
Python Type Conversion and Type Casting
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Tuple count()
Python String casefold()
Python String upper()
Python object()
Python String isalnum()
Python Program to Display Powers of 2 Using Anonymous Function
Python Set update()
Python Program to Display Calendar
Python Objects and Classes
Python List clear()
Python Closures
Python Set isdisjoint()
Python Program to Capitalize the First Character of a String
Python max()
Python Global, Local and Nonlocal variables
Python range()
Python String islower()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python vars()
Python String rfind()
APIs in Node.js vs Python - A Comparison
Python Dictionary copy()
Python String ljust()