Calculate Body Mass Index (BMI)

Calculate BMI from weight (kg) and height (meters) and classify the result.

BeginnerTopic: Basic Python Programs
Back

What You'll Learn

  • Using exponentiation for squared values
  • Combining numeric computation with classification logic
  • Applying real-world thresholds in code

Python Calculate Body Mass Index (BMI) Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to calculate Body Mass Index (BMI)

weight = float(input("Enter your weight in kg: "))
height = float(input("Enter your height in meters: "))

if height <= 0:
    print("Height must be greater than zero.")
else:
    bmi = weight / (height ** 2)
    print("Your BMI is:", round(bmi, 2))

    if bmi < 18.5:
        print("Category: Underweight")
    elif bmi < 25:
        print("Category: Normal weight")
    elif bmi < 30:
        print("Category: Overweight")
    else:
        print("Category: Obesity")
Output
Enter your weight in kg: 68
Enter your height in meters: 1.75
Your BMI is: 22.2
Category: Normal weight

Step-by-Step Breakdown

  1. 1Read weight and height from the user.
  2. 2Validate that height is positive.
  3. 3Compute BMI as weight / (height ** 2).
  4. 4Round and print the BMI value.
  5. 5Use if-elif-else to print the BMI category.

Understanding Calculate Body Mass Index (BMI)

BMI is calculated as:

\[ BMI = \frac{\text{weight}}{\text{height}^2} \]

Then we classify based on standard thresholds:

< 18.5 → Underweight
18.5–24.9 → Normal weight
25–29.9 → Overweight
≥ 30 → Obesity

This program combines arithmetic, conditional logic, and basic validation.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Practical Learning Notes for Calculate Body Mass Index (BMI)

This Python program is part of the "Basic Python Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.