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 items()
Python Program to Check the File Size
Python classmethod()
Python String isalpha()
Python Set symmetric_difference()
Python map()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python issubclass()
Python Program to Make a Flattened List from Nested List
Python dir()
Python String format_map()
Python String splitlines()
Python Program to Find the Sum of Natural Numbers
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Dictionary
Python Program to Display Fibonacci Sequence Using Recursion
Python String format()
Python Program to Merge Mails
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Delete an Element From a Dictionary
Deep Learning with Python - Francois Chollet
Python String title()
Python String maketrans()
Python List clear()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Dictionary values()
Python Program to Find LCM
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Find ASCII Value of Character
Python String rjust()