BMI Calculation With Python
We will write some code using the print function, variable creation, input function, data types, type casting, and operators of the Python programming language.
As a result, you will gain a good understanding of Python syntax very quickly. We will determine a person’s BMI through Python coding. BMI (Body Mass Index) indicates whether a person’s body weight is adequate compared to their body height. A normal range of BMI for a human body is 18.5 – 24.9. Our formula for BMI is weight / height². That is, BMI is obtained by dividing body weight by the square of body height.
BMI = (weight)kg / (height)m²
Here, we use our weight in kg and height in meters.
For this program, first of all, we need the user’s weight and height, so we need to use the input function to get the user’s data. As we know, any data we get from the input function is received as a string data type. To avoid this problem, we need to type cast, meaning we need to convert the data type from string to integer or float.
Let’s move to our code:
weight = float(input('Enter your weight in kg: '))
height = float(input('Enter your height in meters: '))
H = height ** 2
BMI = float(weight//H)
print(BMI)
output
Enter your weight in kg: 70
Enter your height in meters: 1.7
24.0
Here, we can see we have three variables: weight, height, H, and BMI. In the weight and height variables, we store data received from the user using the input function. This data type was string, so we typecast it to a float value because we can’t calculate string data. We don’t use the int function because our height or weight values could be float values. We could use the int function, but then we couldn’t use float values and would have to use rounded values.
The H variable holds the value of the square of body height.
Then, we simply divide the body weight by the square of body height. Note that the division operator is (/), but here we use (//) to get a round number. Please try this out practically to find the difference.
After that, we simply use the print function to print the ultimate result.
Above, we mentioned the term “round number” several times. Now, let’s discuss the round function. When we don’t want to take the next decimal place of a decimal number, or we want the nearest whole number, or we want a specific number of digits after the decimal, we can use the round function.
Round function:
print(round(3.712))
print(round(3.421))
print(round(3.5))
print(round(4.5))
print(round(4.5290, 2))
print(round(605, -1))
print(round(655, -2))
OUTPUT
4
3
4
4
4.53
600
700
For the first one, we can see that the round function prints the nearest integer to the given number.
For the second one, it also prints the nearest integer.
The third one was 3.5, and the round function makes it 4 because its nearest even integer is 4.
The fourth one was 4.5, but it is the same as the previous one, with the nearest even integer being 4.
In the fifth one, we can see that we input a limit on how many digits should be printed after the decimal.
The sixth one rounds to the nearest ten.
The seventh one rounds to the nearest hundred.
That’s all about the round function.
Artificial Intelligence in the Aviation and Airlines Sector
AI is transforming the aviation industry, enhancing safety, operational efficiency, and customer experience. Sazit Suvo Designer & Editor Aviation Sector and…
Who Is an AI Engineer? Career, Work, Challenges, and Required
AI Engineer is one of the most in-demand careers today. An AI Engineer designs, develops, and implements artificial intelligence (AI)-based…
Top SQL Keywords and Functions: Master 80% of Data Engineering
SQL is a fundamental skill for data engineering and analytics. With the right understanding of a few key concepts, you…
Statistics and Machine Learning: Relationship and Importance
Statistics and Machine Learning Although statistics and machine learning may sound different, they are deeply related. On one hand, statistics is…
Machine Learning and Deep Learning :Step-by-Step Learning Guide.
Machine Learning (ML) and Deep Learning (DL) Step-by-Step Learning Guide and Job Preparation Are you interested in learning Machine Learning or…
Do You Need to Learn Math First to Start Data
Should You Learn Math First or Alongside ML? “Do I need to learn Math first to start Data Science or Machine…
3 Comments