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 Set pop()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python String replace()
Python Program to Shuffle Deck of Cards
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python print()
Python Variables, Constants and Literals
Python Program to Get the Class Name of an Instance
Python List append()
Python Recursion
Python String rindex()
Python Program to Check If a String Is a Number (Float)
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python type()
Python setattr()
Python String find()
Python Package
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Data Structures and Algorithms - Benjamin Baka
Python frozenset()
Python Numbers, Type Conversion and Mathematics
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python list()
Python Dictionary keys()
Python String isdigit()
Python Program to Display the multiplication Table
Python List
Python range()
Python File I/O Operation
Python Machine Learning - Sebastian Raschka
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python len()