Editor

Python Program to Calculate BMI

Beginner · Basics

Body Mass Index (BMI) is a rough check of whether a person's weight suits their height: the weight in kilograms divided by the square of the height in metres. This program works out the BMI and places it in one of the standard adult categories: underweight, normal weight, overweight or obese.

Program

bmi_calculator.py
weight = 72 # in kilograms, or: weight = float(input("Weight in kg: "))
height = 1.72 # in metres, or: height = float(input("Height in metres: "))
bmi = weight / (height ** 2)
print("BMI:", round(bmi, 1))
if bmi < 18.5:
category = "Underweight"
elif bmi < 25:
category = "Normal weight"
elif bmi < 30:
category = "Overweight"
else:
category = "Obese"
print("Category:", category)
Output
BMI: 24.3
Category: Normal weight

How it works

  1. height ** 2 squares the height, and dividing the weight by it gives the BMI in kilograms per square metre.
  2. round(bmi, 1) keeps one decimal place for display, while the full value is used for the checks.
  3. The if/elif chain tests the cut-offs from lowest to highest, so each check only needs an upper limit: by the time bmi < 25 is tested, the BMI is already known to be at least 18.5.
  4. These are the WHO cut-offs for adults; for children and teenagers, BMI is read from charts made for their age instead.

Another way to write it

For several people, with height in centimetres

For several people, with height in centimetres
def bmi(weight_kg, height_cm):
height_m = height_cm / 100 # 172 cm is 1.72 m
return weight_kg / (height_m ** 2)
people = [("Aditi", 48, 165), ("Rahul", 72, 172), ("Vikram", 98, 178)]
for name, weight, height in people:
print(name, "has a BMI of", round(bmi(weight, height), 1))
Output
Aditi has a BMI of 17.6
Rahul has a BMI of 24.3
Vikram has a BMI of 30.9

Most people know their height in centimetres, so the function divides it by 100 first. Putting the formula in a function also makes it easy to work out the BMI of a whole list of people.

Try it yourself

Change the cut-offs to the ones often used for Asian adults (overweight from 23, obese from 25) and see how the category for this sample changes.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises