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 replace()
Python Program to Check If a List is Empty
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python frozenset()
Python range()
Python String split()
APIs in Node.js vs Python - A Comparison
Python Input, Output and Import
Python staticmethod()
Python bytes()
Python map()
Python Set remove()
Python String lower()
Python hash()
Python Program to Display the multiplication Table
Python Program to Calculate the Area of a Triangle
Python Dictionary values()
Python hasattr()
Python String rpartition()
Python Package
Python String format_map()
Python Program to Remove Punctuations From a String
Python Strings
Python sleep()
Python chr()
Python Dictionary pop()
Python setattr()
Python Anonymous / Lambda Function
Python Program to Convert String to Datetime
Python Program to Find Factorial of Number Using Recursion
Python Program to Convert Decimal to Binary, Octal and Hexadecimal