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 float()
Python Program to Sort Words in Alphabetic Order
Python datetime
Python Machine Learning - Sebastian Raschka
Python Program to Delete an Element From a Dictionary
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Get Line Count of a File
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Program to Print Colored Text to the Terminal
Python format()
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python String zfill()
Python String capitalize()
Python String join()
Python String format_map()
Python Program to Create a Countdown Timer
Python String title()
How to get current date and time in Python?
Python Objects and Classes
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python String count()
Python Program to Count the Occurrence of an Item in a List
Python Program to Split a List Into Evenly Sized Chunks
Python Program to Check If a String Is a Number (Float)
Python String translate()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Deep Learning in Python - LazyProgrammer
Python String isidentifier()
Python compile()
Python Program to Iterate Through Two Lists in Parallel
Python len()
Python dir()