TØ 1. Getting started
Python is a programming language that is widely used in the scientific community (and for many other things). You will be using Python in various contexts during your studies - ranging from data analysis through plotting to bioinformatics.
At first Python may seem strange and difficult - it is a different way to think compared to what you might be used to. However, with practice and patience you will become familiar with it and be able to use it to learn and do science in ways that would not be possible without it.
1 Programming languages
You have almost certainly used something that works like a programming language before — you may simply not have thought about it that way. For example, a calculator uses a programming language of sorts - it has a set of inputs (numbers) and a set of operations (add, substract, multiply, divide, etc) that can be composed together to perform a calculation. Python can do the same, and much more, for example
This is an interactive code-block that runs Python in the browser. You can click the Run Code button to perform the calculation.
1.1 Exercise: Change the calculation.
You can edit the code in the cell above just click on the the line with code and you can start typing. Try for example calculating \(1 + 2 + 3 + 4\) rather than the original calculation.
1.2 Exercise: Other programming languages
If we define a programming language as
A programming language is a way of writing precise instructions that tell a computer what operations to perform and in what order.
Think of other systems you have used that might qualify as a programming language. They should allow you to combine operations or instructions to produce a result.
2 Python as a calculator
All operations you’re used to for a regular handheld calculator exist in Python
1 + 1 # Addition with +
3 - 1 # Subtraction with -
4 * 4 # Multiplication with *
20 / 5 # Division with /2.1 Exercise: Calculate, calculate, calculate
Use the interactive cell below to perform the following calculations
- \(21 + 21\)
- \(53 - 11\)
- \(6 \times 7\)
- \(\frac{546}{13}\)
3 Calculations & Parentheses
In later courses you will need to work with equations such as the one shown below
\[ \theta = \frac{K_D + [P_{tot}] + [L_{tot}]}{2[P_{tot}]} - \sqrt{\left(\frac{K_D + [P_{tot}] + [L_{tot}]}{2[P_{tot}]}\right)^2 - \frac{[L_{tot}]}{[P_{tot}]}}, \] which can be used to describe titration data that reports protein saturation as a function of total ligand concentration.
Calculating this function requires that we are careful with parentheses! Which we have all learned, but since it is very important it is worth refreshing.
3.1 Exercise: Mind the (…)
For the following pairs of expressions, calculate both (by hand or mentally, according to preference.)
\[ \begin{aligned} 2 + 3 \times 4 \quad &\mathrm{vs.} \quad (2 + 3) \times 4 \\ \\ 10 - 2^2 \quad &\mathrm{vs.} \quad (10 - 2)^2 \\ \\ 100 / 10 \times 2 \quad &\mathrm{vs.} \quad 100 / (10 \times 2) \\ \\ \frac{12 + 8}{4} \quad &\mathrm{vs.} \quad 12 + 8 / 4 \end{aligned} \]
3.2 Exercise: Beware of the (…)
For each of the interactive cells below, consider what the result will be before you run the cell.