Super Simple Python: Unit Convertor

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to build a simple unit convertor in under 25 lines of Python!

For a video version:

Unit conversion is annoying for humans at best, but very simple for machines. If you went to school in America, you probably wasted a year of science class learning how to do unit conversions. We’re going to build a unit convertor in Python in much the same way we built the Super Simple Python Calculator. We’ll build a set of functions and then use a dictionary to map strings to those functions.

Unit Convertor Functions

In the video I only gave four functions, but in this example, I’ll give six. We’ll do inches to feet, miles to kilometers, celsius to fahrenheit, and all the reverses. Each of these functions is pretty self-explanatory by name. They will each take one parameter – the value in the base unit – and return the value in the converted to unit. Each function will just do some math on the passed in value to convert the unit over.

def in_to_ft(_in):
    return _in/12
 
def ft_to_in(_ft):
    return _ft*12
 
def mi_to_km(_mi):
    return _mi*1.609
 
def km_to_mi(_km):
    return _km/1.609
 
def c_to_f(_c):
    return 9*_c/5 + 32
 
def f_to_c(_f):
    return (_f-32)*5/9

Mapping the Functions

Much like we did with the calculator function we will now map the functions. Thanks to the way Python is built as a language, we can map strings to functions in a dictionary. Let’s create a dictionary that will take a string of plain English as a key and map that to a function as a value. We made six functions earlier so our dictionary will have six entries.

_dict = {
    "inches to feet": in_to_ft,
    "feet to inches": ft_to_in,
    "miles to kilometers": mi_to_km,
    "kilometers to miles": km_to_mi,
    "celsius to fahrenheit": c_to_f,
    "fahrenheit to celsius": f_to_c    
}

Doing the Unit Conversion

Now that we’ve set up our functions and our mapping, all we have to do is perform the unit conversion. First we’ll tell the user which conversions are available. Then, we’ll ask them which conversion they want to do. We will also need to get the value of the base unit from the user. Notice that I wrap the input() in a float() for getting the base value. This is to convert the inputted string value into a float. Once everything is set up, we simply call the dictionary to get our unit convertor function and print out the converted value.

print("Available conversions (to and from): inches to feet, miles to kilometers, celsius to fahrenheit")
convert = input("Which conversion do you want to do? ")
num = float(input("What is the value in the base unit? "))
 
op = _dict[convert]
print(op(num))

When we run our program, we should see something like the image below.

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

One thought on “Super Simple Python: Unit Convertor

Leave a Reply

%d bloggers like this: