Hey all, this is the second article of the 'Fun Friday' series, where do make some really fun programs! In this article, we are going to make various unit converters in Python. I hope you will love it!
We are going to make different programs for converting length, temperature and pressure. I will be explaining you the core basics of each and every program we are going to built! So get ready with your code editors.
*Note: We won't be using any built-in modules for these programs, we would be doing everything from scratch itself!*
#1: Making Length Converters in Python:
One of the most basic conversion programs is the conversion of length. In this program, we are going to first ask the user for the length, then, what is it's unit, and then, in which unit the user wants it to get converted.
We will be using simple if and else conditions. We are basically going to interconvert the following units: metre, centimetre, kilometre and millimetre.
We would be using basic conversion formulae:
1 centimetre = 10 millimetre
1 metre = 100 centimetre
1 kilometre = 1000 metre
All other conversions can be done using these basic ones.
Here's the code:
# Making lenth converters in python | |
length1= float(input("Please enter the length: ")) | |
unit1= input("Please enter the unit in which your data is: ") | |
unit2= input("Please enter the unit in which you want it to convert: ") | |
if unit1=="metre": | |
if unit2=="metre": | |
print("The data in metres is equal to: ", length1) | |
elif unit2=="centimetre": | |
length2= length1*100 | |
print("The data in centimetres is equal to: ", length2) | |
elif unit2=="kilometre": | |
length2=length1/1000 | |
print("The data in kilometres is equal to: ", length2) | |
elif unit2== "millimetre": | |
length2= length1*1000 | |
print("The data in millimetres is equal to: ", length2) | |
elif unit1=="centimetre": | |
if unit2=="centimetre": | |
print("The data in centimetres is equal to: ", length1) | |
elif unit2=="metre": | |
length2= length1/100 | |
print("The data in centimetres is equal to: ", length2) | |
elif unit2=="kilometre": | |
length2=length1/100000 | |
print("The data in kilometres is equal to: ", length2) | |
elif unit2== "millimetre": | |
length2= length1*10 | |
print("The data in millimetres is equal to: ", length2) | |
elif unit1=="kilometre": | |
if unit2=="kilometre": | |
print("The data in kilometres is equal to: ", length1) | |
elif unit2=="centimetre": | |
length2= length1*100000 | |
print("The data in centimetres is equal to: ", length2) | |
elif unit2=="metre": | |
length2=length1*1000 | |
print("The data in metres is equal to: ", length2) | |
elif unit2== "millimetre": | |
length2= length1*1000000 | |
print("The data in millimetres is equal to: ", length2) | |
elif unit1=="millimetre": | |
if unit2=="millimetre": | |
print("The data in millimetres is equal to: ", length1) | |
elif unit2=="centimetre": | |
length2= length1/10 | |
print("The data in centimetres is equal to: ", length2) | |
elif unit2=="kilometre": | |
length2=length1/1000000 | |
print("The data in kilometres is equal to: ", length2) | |
elif unit2== "metre": | |
length2= length1/1000 | |
print("The data in metres is equal to: ", length2) | |
else: | |
print("Please check your spelling!") |
Does the code seem complex? No, it is not. It looks big because we have to test it for all of those units. You can add more units of length by just copy pasting and a little modification of code. I hope you have understood it by now.
#2 Making Temperature Converters in Python:
In this program, we are going to make a temperature converter in python. We would be interconverting the following units: celsius, fahrenheit and kelvin.
We would would be using these basic conversion formulae:
K = °C + 273.15
°C = (°F * (9/5)) + 32
°F = (°C - 32) * (5/9)
Here's the code:
# Temperature converter in Python | |
temp1 = float(input("Please enter the temperature: ")) | |
unit1 = input("Please enter the unit of your data: ") | |
unit2= input("Please enter the unit you want it to convert in: ") | |
f_to_c = (temp1 - 32)*(5/9) | |
c_to_f = (temp1*(9/5)) + 32 | |
if unit1=="celsius": | |
if unit2 == "celsius": | |
print("The data in celsius is: ", temp1) | |
elif unit2 == "kelvin": | |
temp2 = temp1+ 273.15 | |
print("The data in kelvin is: ", temp2) | |
elif unit2 == "fahrenheit": | |
temp2= c_to_f | |
print("The data in Fahrenheit is: ", temp2) | |
elif unit1=="fahrenheit": | |
if unit2 == "fahrenheit": | |
print("The data in fahrenheit is: ", temp1) | |
elif unit2 == "kelvin": | |
new_temp = f_to_c | |
temp2 = new_temp+ 273.15 | |
print("The data in kelvin is: ", temp2) | |
elif unit2 == "celcius": | |
temp2= f_to_c | |
print("The data in celcius is: ", temp2) | |
elif unit1=="kelvin": | |
if unit2 == "kelvin": | |
print("The data in kelvin is: ", temp1) | |
elif unit2 == "celsius": | |
temp2 = temp1-273.15 | |
print("The data in celsius is: ", temp2) | |
elif unit2 == "fahrenheit": | |
new_temp= temp1-273.15 | |
temp2= (new_temp*(9/5)) + 32 | |
print("The data in Fahrenheit is: ", temp2) | |
else: | |
print("Wrong input!") | |
*Note: To make the program clean, we stored the conversion formulae of °C to °F and vice versa in variables, 'c_to_f' and 'f_to_c'. *
# 3: Making a Pressure Converter in Python:
Hey all, in this program, we are going to make a pressure converter in python. We would be interconverting the following units: pascal, bar and Atmospheric Pressure (atm).
We would be using these basic conversion formulae:
Pascal = bar * 100000
Bar = atm*1.013
Atm = Pascal/101325
Here's the code:
# Pressure Converter in Python | |
pressure1= float(input("Please enter the pressure: ")) | |
unit1 = input("Please enter the unit of your data: ") | |
unit2 = input("Please enter the unit in which you want to convert your data: ") | |
if unit1 == "pascal": | |
if unit2 == "pascal": | |
print("The data in Pascal is: ", pressure1) | |
elif unit2 == "bar": | |
pressure2 = pressure1/100000 | |
print("The data in Bar is: ", pressure2) | |
elif unit2 == "atm": | |
pressure2 = pressure1/101325 | |
print("The data in atmospheric pressure (atm) is: ", pressure2) | |
elif unit1 == "bar": | |
if unit2 == "bar": | |
print("The data in Bar is: ", pressure1) | |
elif unit2 == "pascal": | |
pressure2 = pressure1*100000 | |
print("The data in Pascal is: ", pressure2) | |
elif unit2 == "atm": | |
pressure2 = pressure1/1.013 | |
print("The data in atmospheric pressure (atm) is: ", pressure2) | |
elif unit1 == "atm": | |
if unit2 == "atm": | |
print("The data in atm is: ", pressure1) | |
elif unit2 == "pascal": | |
pressure2 = pressure1/101325 | |
print("The data in Pascal is: ", pressure2) | |
elif unit2 == "bar": | |
pressure2 = pressure1*1.013 | |
print("The data in bar is: ", pressure2) | |
else: | |
print("Wrong Input!") |
--------------------------------
So, that's it for today. Today we learnt the best way of making unit converters in python. I hope you this session was beneficiary for you! Although we have made only 3 programs here, but now that you now how to make unit converters, you can now easily make other programs by your own.
I hope you really liked this tutorial and if you did then don't forget to follow us as we do these coding sessions regularly.
Also read: Writing some fun programs using 'Random' Module in Python
Also, please share this if you liked it! It can help someone struggling in this problem.
Again, thanks for reading!
No comments:
Post a Comment