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 Program to Sort Words in Alphabetic Order
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python List extend()
Python Directory and Files Management
Python Dictionary pop()
Python iter()
Python String title()
Python Dictionary values()
Python Dictionary items()
Python Operators
Python Program to Get the Class Name of an Instance
Python len()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Print all Prime Numbers in an Interval
Python enumerate()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python min()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String splitlines()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python type()
Python List index()
Python Objects and Classes
Python Program to Find the Factorial of a Number
Deep Learning with Python - Francois Cholletf
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Dictionary setdefault()
Python Program to Print Output Without a Newline
Python Statement, Indentation and Comments
Python datetime
Python String isnumeric()
Python Program to Slice Lists