Weather forecasts, climate projections and predictions of how a disease spreads all come from computer models. A model is a simplified version of something real that helps you understand it or predict what it will do. A computer model is a set of rules written as code: start with some measured values, apply a rule (such as "the population grows by 10% each year"), and repeat. Computers can repeat a rule millions of times in a second, so scientists can ask "what if?" and see the answer right away. In this lesson you will read and write short Python programs that model science, using the same coding ideas as in math class.
Variables, arithmetic and print
A variable is a name that stores a value, like a labelled box. The line mass = 54.0 means "put 54.0 in the box called mass"; the = sign assigns a value, it does not mean "equals" as in math. Python does arithmetic with + (add), - (subtract), * (multiply) and / (divide). The print function shows values on the screen; text in quotation marks is printed exactly as written. Anything after a # symbol is a comment: a note for people that Python ignores. In the programs below, the comments at the end show the exact output.
mass = 54.0 # grams
volume = 20.0 # cubic centimetres
density = mass / volume
print("Density:", density, "g/cm3")
# Output:
# Density: 2.7 g/cm3Loops repeat a rule
A for loop repeats the indented lines below it. It can run once for each item in a list (values in square brackets, separated by commas), or once for each number in a range. range(1, 6) gives the numbers 1, 2, 3, 4 and 5: it starts at the first number and stops before the second. The lines that belong to the loop must be indented (pushed in, usually by 4 spaces). Lines that are not indented run once, after the loop has finished.
readings = [12, 15, 20, 25] # temperatures in degrees C
for c in readings:
f = c * 9 / 5 + 32
print(c, "C =", f, "F")
# Output:
# 12 C = 53.6 F
# 15 C = 59.0 F
# 20 C = 68.0 F
# 25 C = 77.0 FA model of a growing population
Suppose 200 rabbits live in a field and the population grows by 10% each year. Growing by 10% means the new population is 100% + 10% = 110% of the old one, so each year you multiply by 1.1. The program below applies that rule five times. round() rounds to the nearest whole number, because you cannot have part of a rabbit. Notice that the increase gets bigger each year (20, then 22, then about 24), because 10% of a bigger number is bigger. This is called exponential growth.
population = 200
for year in range(1, 6):
population = population * 1.1
print(year, round(population))
# Output:
# 1 220
# 2 242
# 3 266
# 4 293
# 5 322Modelling cooling and analysing data
tea = 90.0 # starting temperature of the tea, degrees C
room = 20.0 # room temperature, degrees C
for minute in range(1, 6):
tea = tea - 0.1 * (tea - room)
print(minute, round(tea, 1))
# Output:
# 1 83.0
# 2 76.7
# 3 71.0
# 4 65.9
# 5 61.3trials = [41.2, 39.8, 40.5, 42.1, 40.9] # seconds
total = 0
for t in trials:
total = total + t
mean = total / len(trials)
print("Mean:", round(mean, 2), "s")
# Output:
# Mean: 40.9 s