Python
Contents
Python#
The basic instructions that computers understand are limited and difficult to work with directly. Instead, we write programs in a programming language. The programming language is then converted into the basic instructions, although we usually do not need to worry about that step. There are many programming languages, but a popular choice is Python. Python is widely available, comparatively easy to learn and use, and reasonably good at many mathematical tasks. For this reason, you will likely use it in many of the modules in your degree. Specialized modules may use other languages, but your skills in Python will transfer over.
Jupyter Notebooks#
We will be demonstrating Python inside Jupyter Notebooks. These allow for text and code to be mixed together, and you can run all of the code “inside” the Notebook. You may be viewing these notes as static web-pages instead; a “live” Notebook version will be available on Moodle. There are other methods of using Python, like running it in an “IDE” or directly from a command line, which you may see later in your studies. Notebooks are divided into cells, which may contain text (like this cell) or code. The following cell contains some Python code which just adds together 3 and 4. You can run the cell by selecting it and then clicking the “Run” button at the top, or using the shortcut Ctrl-Enter
.
3 + 4
7
Learning to use the shortcuts is very handy. You can see a list of other shortcuts via the Help
menu. This menu also contains a brief “User Interface Tour” which you should take.
Cells#
To add a new cell, use the Insert
menu or the shortcuts A
and B
, which insert a cell above or below the currently selected cell respectively. You can then change between text (Markdown
) and Code
using the Cell>Cell Type
menu, or using the shortcuts Y
and M
respectively. Markdown is a simple language for formatting text. When you edit a text cell you will see Markdown, and when you run the cell it will “render” the Markdown. Try editing this cell by double clicking it or selecting it and pressing Return
, then running the cell to see the difference. There is a later notebook which discusses how to use Markdown to format your text cells.
Arithmetic#
The simplest way to use Python is just as a fancy calculator. As we saw above, we can add using +
. We can also subtract, multiply, and divide:
3 - 4
-1
3 * -4
-12
3/4
0.75
As the previous output suggests, we can use decimals as normal. We call these floating point numbers or floats for short. All of the operations work on them just like you would expect, though there are some rounding issues that arise when using them that we will discuss later.
0.75 * 2
1.5
In order to take powers, we use **
, like so:
2 ** 3
8
9 ** 0.5
3.0
Warning
You might be familiar with using ^
for powers, but this means something different in Python and will give you strange answers if you misuse it!
2 ^ 3
1
We can do more than one operation in the same line. The order in which operations are carried out follows the usual “BODMAS/PEMDAS” rules; brackets are evaluated first, followed by powers, then multiplication and division, then addition and subtraction. To avoid any confusion it’s often best to emphasize the order using brackets!
(0.5 + 1) * 2 + 1
4.0
We can also do arithmetic with complex numbers. Python uses j
for the imaginary unit \(i\), so you can create complex numbers like so:
0.5 + 6j
(0.5+6j)
Note that it is 6j
and not 6*j
. Arithmetic works as expected:
(1 + 1j) * (1 + 2j)
(-1+3j)
Comments#
Comments are a crucial tool you can use to make your code easier to understand, write, and maintain. Anything after a #
symbol in a code cell will be ignored by Python, and we use this to include descriptions of what our code is doing like so:
# The following comment is not really necessary, but makes a simple example.
# Multiply 1.2 by 43, add seven, and divide the whole thing by 2
(1.2 * 43 + 7)/2
29.3
You can also include commentary about the code in text cells like this, though this is an option that is particular to Jupyter Notebooks while comments are available in any Python code.
Of course, there was no real need to include the comment in the cell above: the code is arguably clearer than the English. You will develop an understanding of how and when to include comments over time. It is best if the comments explain the code rather than duplicating what it says, which is not the case in the example above. As a rule of thumb, comment more rather than less, and certainly comment on code that you had to think about, or that does something “clever”. Anyone else who has to read your code will be grateful (this other person is most likely to be your future self).
A brief introduction to Turtles#
A running example throughout these notes is “turtle graphics”. Try running the code cell below. You should see a turtle move around the screen and trace out a path.
# Turtles don't come in basic Python, we need to get them from a "module"
from mobilechelonian import Turtle
terry = Turtle() # create a turtle, and call him terry
# terry will trace out the path he moves along, like he has a pen attached to him
terry.forward(100) # move terry forward 100 units
terry.left(90) # turn terry left 90 degrees
terry.forward(100) # move terry forward 100 units again
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Input In [13], in <cell line: 2>()
1 # Turtles don't come in basic Python, we need to get them from a "module"
----> 2 from mobilechelonian import Turtle
4 terry = Turtle() # create a turtle, and call him terry
6 # terry will trace out the path he moves along, like he has a pen attached to him
ModuleNotFoundError: No module named 'mobilechelonian'
We’ll explain the meaning of the lines above in later sheets. When a cell has multiple lines of code, as above, Python will run the lines from top to bottom.
TeachingHub etiquette#
If you are running your Notebooks on the TeachingHub (i.e. running them in your web browser, through the link on Moodle), please be mindful of the fact that each Notebook adds some load to the server. Please don’t leave many Notebooks open at once, and after you finish with a Notebook, use “Close and Halt” in the File menu to free up the resources (making sure to save any changes first!). At the end of your session, please logout via the button in the top-right corner.