Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON DATA / CHAPTER 02 / Learn / LESSON 04
Third class. You have a list of weights. You want BMI for each — not one by one by hand, but in a batch.
The formula is familiar from Foundations: BMI = weight / height ** 2. Here height is fixed (1.8), and weight comes from each item in weights.
The pattern is: empty list bmis = [], loop for w in weights:, compute, append, print. append adds the result to the end of bmis so you keep every BMI in one place — not only print them.
Mini example:
height = 1.8
weights = [70.0, 72.0]
bmis = []
for w in weights:
bmi = w / height ** 2
bmis.append(bmi)
print(round(bmi, 2))round(..., 2) again helps readable output. The values inside bmis keep full precision.
Common mistake: forgetting append and overwriting one variable; writing height ** 2 as height * 2; looping over a number instead of the list.
Learn: you write the variables and the loop; the formula is in the task.
BATCH IDLE
›