Making various unit converters in Python! - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Friday 9 October 2020

Making various unit converters in Python!


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 PythonI 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:


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: 


*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:


--------------------------------


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