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 slice()
Python String rjust()
Python Program Read a File Line by Line Into a List
Python RegEx
Python Type Conversion and Type Casting
Python Object Oriented Programming
Python Generators
Python String join()
Python Get Current time
Python Program to Print Colored Text to the Terminal
Python len()
Python Program to Append to a File
Python Program to Display Calendar
Python Set symmetric_difference_update()
Python Program to Split a List Into Evenly Sized Chunks
Python ascii()
Python Dictionary pop()
Python Program to Find the Square Root
Python Objects and Classes
Java Program to Implement the Program Used in grep/egrep/fgrep
Python String count()
Python Program to Parse a String to a Float or Int
Python Dictionary popitem()
Python Custom Exceptions
Python setattr()
Python List copy()
Python repr()
Python Program to Check If a String Is a Number (Float)
Python Program to Print all Prime Numbers in an Interval
Python tuple()
Python Statement, Indentation and Comments
Python List reverse()