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 Dictionary setdefault()
Python Dictionary update()
Python Program to Convert Two Lists Into a Dictionary
Python vars()
Python Program to Find Sum of Natural Numbers Using Recursion
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python str()
Python String rstrip()
Python List insert()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python List clear()
Python Program to Transpose a Matrix
Python String replace()
Python locals()
Python Program to Find the Largest Among Three Numbers
Python Program to Convert String to Datetime
Python Program to Check If a String Is a Number (Float)
Python Program to Check if a Number is Positive, Negative or 0
Python all()
Deep Learning with Python - Francois Chollet
Python Dictionary copy()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Set intersection_update()
Python Program to Find LCM
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Set difference_update()
Python format()
Python Program to Return Multiple Values From a Function
Python Input, Output and Import
Python for Loop
Python exec()
Python String rindex()