TØ 3. Variables & Functions
In this session we will explore variables and functions - both of which are important elements of Python (and many other programming languages)
1 Variables
To really unlock the utility of Python variables are required, among other things they allow us to express calculations in a simpler way. A variable is defined by using =, for example, we can define
a = 21 + 21This means that the result of the calculation is stored in the variable a and can be used in subsequent calculations.
1.1 Exercise: Calculations with variables
Repeat the calculations
- \(21 + 21\)
- \(53 - 11\)
- \(6 \times 7\)
- \(\frac{546}{13}\)
But now assign each of them to a variable a, b c etc and then calculate
\[
(21 + 21) + (53 - 11) + (6 \times 7) + \frac{546}{13}
\] using these variables. In the cell below replace ______ with your code
The cell above used a new thing print. This is a function, functions are like recipe - they take a number of inputs (ingredients) and perform some predefined set of operations on those and return an output.
The print-function prints what it is given to the screen - in this case the five variables. You will see and use the print-function numerous times.
1.2 Exercise: A biological calculation
The number of cells in different parts of the body is given below
| Type | Number of cells |
|---|---|
| Total cells | \(37 \cdot 10^{12}\) |
| Brain cells | \(86 \cdot 10^{9}\) |
| Liver cells | \(240 \cdot 10^9\) |
| Skin cells | \(16 \cdot 10^{11}\) |
For each cell type, calculate the percentage each category represents of the total number of cells in the body, i.e., for each category calculate
\[ P = \frac{\mathrm{Number}}{\mathrm{Total}} \times 100. \]
The numbers of cells are rather large, so typing the correct amount of zeros is a little painful. The code below shows four different ways of writing the same number in Python code
13000000000000 # Basic
13_000_000_000_000 # Underscores to visually distinguish
13 * 10**(12) # Using exponentation
13E12 # Scientific notation2 Functions
When doing repeated calculations it is generally good practice to define a function. Consider for example that you need to calculate the average of several sets of numbers, without using functions we could write
A = 1
B = 2
C = 3
D = 4
average_AB = (A + B) / 2
average_BC = (B + C) / 2
average_CD = (C + D) / 2This is perfectly valid, but it is more prone to error - as we have to write the expression multiple times - and it is harder for someone else to decipher - as they have to check they understand what each line of code does. If we instead used a function
A = 1
B = 2
C = 3
D = 4
def average(x, y):
return (x + y) / 2
average_AB = average(A, B)
average_BC = average(B, C)
average_CD = average(D, C)Now all the logic of calculating the average is contained in the average function so we only have one place to check for errors.
Functions use specific syntax, meaning they have to be written following a set of rules
- 1
-
The function is defined with
defand its name (herepercentage) and its inputs(number, total). - 2
- The body of the function is indented by 4 spaces (or 1 tab). The body can consist of as many lines as needed.
- 3
-
returnexits the function and returns the variable that follows - herefinal_result. If a function doesn’t have areturnor just hasreturnwithout any variables it implicitly returnsNone.
Using a function requires “calling” it, this means giving it the inputs it expects - like below
result = percentage(1, 2)This code will calculate the function with number=1 and total=2 and assign the returned value to result. Outside the function the variables intermediate_calculation and final_result are not available (we would say they are out of scope). There’s also nothing special about the names, they are just variables like anywhere else, they could just aswell have been called nemo_the_fish or min_endelige_beregning_af_gennemsnittet_foretaget_mandag_den_13_april but its a good idea to give variables descriptive but short names.
Only what was returned by the function is available outside the function and if it needs to used later on in the code it needs to be saved to its own variable, so we can think of the code above like
percentage(1, 2)calculates and returnsfinal_result.- Whatever is returned is assigned to the variable
result.
2.1 Exercise: A biological calculation with a function.
Repeat the calculation of cell percentages but now using the percentage function defined below
2.2 Exercise: Fix the (…)
We want to calculate
\[ \frac{a + b}{c + d} \]
Someone has implemented the function below to do so, but it is incorrect - your task is to fix it.
2.3 Implement the (…)
The first term of the quadratic binding equation1 is
\[ \frac{K_D + [P_{tot}] + [L_{tot}]}{2[P_{tot}]} = \frac{a + b + c}{2 b} \]
Complete the function below so it calculates this expression
Footnotes
An equation that is central to enzyme kinetics, which you will learn more about in later courses.↩︎