01
Python quantities, variables and parameters
In this lesson, we use a small SIR epidemic scenario to learn how Python stores biological quantities and model parameters. You can change the code, run it in the page and see the outcome immediately.
Biological scenario
An infection has entered a closed population of 1,000 people. At the beginning, 990 people are susceptible, 10 are infectious and nobody has recovered.
We want to store these quantities in Python and calculate the initial infection and recovery rates.
Mathematical-biology context
The SIR model divides the population into three compartments:
- \(S\): number of susceptible people;
- \(I\): number of infectious people;
- \(R\): number of recovered people.
\[S(0)=990,\qquad I(0)=10,\qquad R(0)=0.\]
The total population is
\[N=S+I+R=1000.\]
We use \(\beta=0.3\) as the transmission parameter and \(\gamma=0.1\) as the recovery-rate parameter. Their precise biological interpretation will be developed in later lessons.
Programming objective
We will store the compartment sizes and parameters, calculate useful quantities and check that the initial values are biologically valid.
Build the code step by step
Step 1: Store the compartment sizes
S = 990
I = 10
R = 0
S, I and R are variable names. The assignment operator = stores a value under a name. Here the values are integers because they count people.
Step 2: Store the parameters
beta = 0.3
gamma = 0.1
beta and gamma are model parameters. Their decimal values are stored as floating-point numbers.
Step 3: Calculate derived quantities
N = S + I + R
infection_rate = beta * S * I / N
recovery_rate = gamma * I
* means multiplication and / means division. Python evaluates the right-hand side first and assigns the result to the name on the left.
Step 4: Check the model conditions
assert S >= 0 and I >= 0 and R >= 0
assert S + I + R == N
assert checks whether a condition is true. The compartments cannot contain negative numbers, and their sum must equal the total population.
Run the complete program
Edit any value and select Run code. The output will appear below the program. Python may take a few seconds to load the first time.
Output
Run the code to see the result.
Expected outcome
| Quantity | Value | Meaning |
|---|---|---|
| Total population | 1,000 | Everyone in the three compartments |
| Infectious percentage | 1.0% | Initial proportion that is infectious |
| Initial infection rate | 2.97 people/day | Instantaneous model rate of new infections |
| Initial recovery rate | 1.00 person/day | Instantaneous model rate of recovery |
Biological interpretation
Initially, 1% of the population is infectious. The model gives an infection rate of 2.97 people per day and a recovery rate of 1 person per day. Because the infection rate is greater than the recovery rate at this initial state, the infectious population is expected to increase initially.
These are continuous model rates. They do not mean that exactly 2.97 people become infected during a particular day.
Important technical points
- A variable is a name referring to a stored value.
- A parameter is a quantity that controls model behaviour.
- Python is case-sensitive: I and i are different names.
- The name on the left of = receives the value calculated on the right.
- All compartment values must be non-negative.
- The total population must satisfy \(N=S+I+R\).
Try it yourself
Change the initial infectious population from 10 to 25. Change the susceptible population from 990 to 975 so that the total remains 1,000.
- Predict whether the initial infection rate will increase or decrease.
- Run the program.
- Compare the infection and recovery rates.
- Explain the result in biological terms.