Python for Absolute Beginners: Mathematics with a BMI Calculator.

Kasim Ali
7 min readMar 18, 2023

Are you ready to dive into the world of Python and discover how arithmetic operators work? Brace yourself for an exciting journey where you’ll learn how to crunch numbers and build your very own BMI calculator. Get your thinking cap on and let’s start exploring the mathematical wonders of Python!

Per usual, if you’d like to keep pace with a video tutorial, simply click here.

You can also find the course notes here and the Replit here.

Get ready to unleash your inner math wizard with Python! With just addition, subtraction, multiplication, and division, we can perform all sorts of incredible calculations.

In this tutorial, we’ll show you some amazing examples of how Python can solve mathematical problems with ease.

But wait, it gets even better! We’re going to dive into a real-life scenario that will put our math skills to the test.

Picture this: a trip to the supermarket where Julie grabs 5 apples and Timothy takes 7.

Now, we’ll need to flex our computational muscles to figure out how many apples they have combined.

Sure, we could do it in our heads, but where’s the fun in that? Let’s use Python to make things a little more exciting!

You can follow along on line 3 of the Replit:

julie_apples = input('How many apples does Julie have?')
timothy_apples = input('How many apples does Timothy have?')

total_apples = julie_apples + timothy_apples

print(total_apples)

If you did actually run this, you would run into a problem that I eluded to in my most recent video.

The input function by default takes in only strings as input and if we were to use the + operator with strings we would be using concatenation and not addition.

You can overcome this minor inconvenience by using the int() function which converts its argument to an integer data type.

Amend your code using the Replit to look like this:

julie_apples = int(input('How many apples does Julie have?'))
timothy_apples = int(input('How many apples does Timothy have?'))

total_apples = julie_apples + timothy_apples

print(total_apples)

Or if you only wanted to convert the data type before you use it instead of at a variable level you could do the following:

julie_apples = input('How many apples does Julie have?')
timothy_apples = input('How many apples does Timothy have?')

total_apples = int(julie_apples) + int(timothy_apples)

print(total_apples)

As you get more experience using Python, you will gain an intuition on when to convert the data type.

Let's try something a little bit more complicated.

Julie and Timothy happen to walk back from the supermarket and get very hungry.

For every kilometre they walk, they eat a total of 1 apple. How do you think we could program this using Python?

The math would look something like this:

(Julies apples + Tims apples) — KM walked

julie_apples = input('How many apples does Julie have?')
timothy_apples = input('How many apples does Timothy have?')
km_walked = input('How many KM did they walk?')

total_apples = (int(julie_apples) + int(timothy_apples)) - int(km_walked)

print(f'They have a total of {total_apples} apples')

Now things are starting to get more complicated. We wrapped our initial addition in brackets and then subtracted the number of kilometres walked. (Remember PEMDAS)

We then printed a nice f-string that tells us how many apples are remaining.

Try doing the same in the Replit and remember to use an f-string, convert the data type to an integer and wrap the math part in brackets correctly.

Let’s add one more layer of complexity to this before we build our BMI calculator.

Let’s say that for every 1KM walked, we lose .25 apples.

Follow along on the Replit and make something like this:

julie_apples = input('How many apples does Julie have?')
timothy_apples = input('How many apples does Timothy have?')
km_walked = float(input('How many KM did they walk?'))
apples_lost = km_walked * .25

total_apples = (int(julie_apples) + int(timothy_apples)) - apples_lost

print(f'They have a total of {total_apples} apples')

Notice that we have now converted the km_walked to a float data type at the variable level and we have replaced the latter part of our mathematical equation with apples_lost.

This works out much better for our users because they can now enter the exact amount of KM that Julie and Tim have walked and get a more accurate number for how many apples are left.

We also multiplied using the * operator and simply multiplied the km_walked by the rate of apple loss per KM.

In less abstract terms this looks like this:

2KM * .25 = .5 apples lost.
20KM * .25 = 5 apples lost.
4KM * .25 = 1 apple lost.

We could have also used the / operator which is capable of division. But, this is less clear than using the * operator.

Here are some notes to help you:

  • + operator is used for addition.
  • - operator is used for subtraction.
  • / operator is used for division.
  • * operator is used for multiplication.

There is one other operator you need to know before we make our BMI calculator and that is the square operator. You use this in an interesting way though so take a look below:

print(2*2)    #4
print(2**2) #4

These might look exactly the same but they are very different. The former is simple multiplication.

The latter is squared by the power of 2.

If you wanted to square by the power of 3 you would simply:

print(2**3) #8

And you guessed it, to the power of 4 we would do:

print(2**4) #16

Consider the formula for calculating BMI which is:

BMI = weight (KG) / height squared (M)

We need to use kilograms and metres for this.

Ideally, we want to ask the user for their weight in KG and then their height in M.

Remember you would likely want their weight as an integer and their height as a float.

We could even add some flair to our program by asking for more useful information and printing it at the end.

Try your best in the Replit to do this and come back here once you have completed it or if you are finding it very difficult.

BMI Calculator Answer.

username = input('What is your name?')
gender = input('What is your gender?')

weight_kg = int(input('How much do you weigh? [KG]'))
height_m = float(input('How tall are you? [M]'))

BMI = weight_kg / (height_m ** 2)

print(f'{username} has a BMI of: {BMI}.')

There are 3 important things to note here.

  1. I converted the data type at the variable level for weight_kg.
  2. I converted the data type at the variable level for height_m.
  3. I wrapped height_m ** 2 in its own parentheses.

** I did not need to wrap it in its own parentheses but don’t you think it looks a lot more readable this way? **

Test your code using the following values:

  1. 60KG, 1.6M. You should get 23.44
  2. 90KG, 1.2M. You should get 62.5
  3. 110KG, 1.9M. You should get 30.47

Making it even better.

You might have gotten a BMI that is several decimal places too many. As you can see below.

We can use the round() function.

To use this function you need to pass it a value you want to round. In our case, you pass BMI and save it to a new variable.

rounded_BMI = round(BMI)

You then give it a second argument which is the number of decimal places you want to round to.

1 or 2 should be fine.

username = input('What is your name?')
gender = input('What is your gender?')

weight_kg = int(input('How much do you weigh? [KG]'))
height_m = float(input('How tall are you? [M]'))

BMI = weight_kg / height_m ** 2

rounded_BMI = round(BMI, 2)

print(f'{username} has a BMI of: {rounded_BMI}')

Take a look at what your program looks like now.

A few more things you need to clean up here:

The formatting of the input is quite messy and does not look good.

You did not make use of all the input data you collected.

You should fix that.

Try and spot the differences below:

username = input('What is your name?\n')
gender = input('What is your gender?\n')

weight_kg = int(input('How much do you weigh in KG?\n'))
height_m = float(input('How tall are you in metres?\n'))

BMI = weight_kg / height_m ** 2

rounded_BMI = round(BMI, 2)

print(f'{username} has a BMI of: {rounded_BMI}. They are {gender}, {height_m}M and {weight_kg}KG.')

I have added a ‘\n’ before the end of each input string. This signifies to Python that we should start a new line which results in the console looking like this:

Well done! You successfully made it all the way through this guide. I hope you thoroughly enjoyed it.

Please consider subscribing to my YouTube and becoming a follower here on Medium.

--

--