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 Directory and Files Management
Python staticmethod()
Python Dictionary fromkeys()
Deep Learning in Python - LazyProgrammer
Python Statement, Indentation and Comments
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Create Pyramid Patterns
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String replace()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python zip()
Python Program to Convert Decimal to Binary Using Recursion
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String count()
Python Program to Multiply Two Matrices
Python Dictionary
Python Set union()
Python Program to Get the Class Name of an Instance
Python Program to Extract Extension From the File Name
Python Program to Find the Sum of Natural Numbers
Python float()
Python Input, Output and Import
Python Set add()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python List extend()
Python Program to Parse a String to a Float or Int
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Deep Learning with Python - Francois Chollet
Python Machine Learning Eqution Reference - Sebastian Raschka
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String startswith()
Python Iterators