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 index()
Python Program to Find the Factors of a Number
Python Program to Get the File Name From the File Path
Python Program to Print Hello world!
Python Program to Count the Number of Digits Present In a Number
Python __import__()
Python Program to Check If a String Is a Number (Float)
Python String lstrip()
Python String format()
Python Program to Display Powers of 2 Using Anonymous Function
Python String lower()
Python Global, Local and Nonlocal variables
Python Program to Generate a Random Number
Python Set pop()
Python Set remove()
Python Get Current time
Python String isspace()
Python Program to Find the Size (Resolution) of a Image
Python List count()
Python Set add()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String partition()
Python Program to Find Numbers Divisible by Another Number
Python Anonymous / Lambda Function
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Inheritance
Python Program to Delete an Element From a Dictionary
Python Program to Create a Long Multiline String
Python String split()
Python Program to Convert Two Lists Into a Dictionary
Python object()
Python pow()