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 classmethod()
Python Program to Shuffle Deck of Cards
Python Program to Check Prime Number
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Set update()
Python Program to Create a Countdown Timer
Python Global, Local and Nonlocal variables
Python format()
Python Strings
Python if...else Statement
Python slice()
Python String rfind()
Python String split()
Python Program to Generate a Random Number
Python Directory and Files Management
Python Program to Count the Number of Occurrence of a Character in String
Python Keywords and Identifiers
Python Statement, Indentation and Comments
Python Program to Calculate the Area of a Triangle
Python Operator Overloading
Python dir()
Python Shallow Copy and Deep Copy
Python String isspace()
Python range()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Dictionary fromkeys()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python open()
Python List copy()
Python strftime()
Python Set remove()