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 Matrices and NumPy Arrays
Python issubclass()
Python Machine Learning - Sebastian Raschka
Python Shallow Copy and Deep Copy
Python Program to Safely Create a Nested Directory
Python String format_map()
Python Variables, Constants and Literals
Python Set isdisjoint()
Python Program to Convert Celsius To Fahrenheit
Python Dictionary update()
Python bytes()
Python Program to Generate a Random Number
Python Program to Concatenate Two Lists
Python Multiple Inheritance
Python Dictionary
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Add Two Numbers
Python Inheritance
Python Program to Get a Substring of a String
Python Program to Display the multiplication Table
Python Program to Trim Whitespace From a String
Python String lower()
Deep Learning with Python - Francois Chollet
Python Program to Check if a Key is Already Present in a Dictionary
Python List Comprehension
Python Objects and Classes
Java Program to Implement the Program Used in grep/egrep/fgrep
Python open()
Python while Loop
Python Program to Check Armstrong Number
Python ord()
Python Program to Make a Simple Calculator