Introduction to Python¶

This notebook should serve as a reminder of some basic ideas about Python, and an introduction to the use of Visual Studio Code. It covers roughly the same ground as this video.

This file is a Jupyter notebook, with file extension .ipynb. It contains a mixture of Python code in code cells, and text in Markdown cells (like this one). You can edit a markdown cell by double-clicking it, and then display the nicely formatted version again by typing SHIFT-ENTER. Markdown cells can contain headings, lists, tables, links and so on, and can use LaTeX notation for mathematics. You can find more details at markdownguide.org.

In the version of this notebook that you can download from the course web page, the code cells do not have any output. You should work through the notebook, clicking on each code cell and typing SHIFT-ENTER to see what output appears. Ideally you should try to work out for yourself what the output will be, and then check whether you were right. You should also explore by altering the given code in various ways to see what happens.

We first recall how to do basic arithmetic in Python. Note that we use a single star for multiplication and a double star for raising to a power.

In [ ]:
print(2 + 2)
print(11 * 41 * 271 * 9091)
print(111111111 ** 2)

Above we used explicit print() statements. If we do the same calculations without print() statements, then Python will still print the last thing that it evaluated, but the other two results will be invisible.

In [ ]:
2 + 2
11 * 41 * 271 * 9091
111111111 ** 2

To divide $a$ by $b$ giving a floating point answer, we just enter a / b. If we instead want to do integer division with a quotient and remainder, we enter a // b for the quotient and a % b for the remainder. Remember that Python treats anything coming after a # character as a comment, and so ignores it.

In [ ]:
print(42 / 10)  # division with a floating point answer
print(42 // 10) # division with an integer answer
print(42 % 10)  # modulo, remainder of division

Here is an example where we need brackets. You should try typing an expression like this yourself, and observe what VS Code does while you are typing. If you type an open bracket, then the editor will automatically insert the corresponding closing bracket. Matching pairs of brackets will be coloured in the same colour. If you have any non-matching brackets, they will be coloured in red with a little squiggle underneath to indicate the error. This is just the first example of the many ways in which VS Code will try to help you and prevent you from going wrong.

In [ ]:
(((3 + 7 + 10) * (1000 - 8)) / (900 + 90 + 2)) - 17 # extra credit if you know the origin of this calculation

Here is an example of a Markdown cell containing LaTeX. If you double-click on it you will see the raw LaTeX code, and if you then type SHIFT-ENTER you will see nicely formatted mathematics again.

The theorem of Pythagoras says that if a right-angled triangle has sides $a$, $b$ and $c$, with $c$ being the hypotenuse, then $a^2 + b^2 = c^2$. As an example of this, we can consider the case where $(a,b,c) = (3,4,5)$. We will check the relevant calculations using Python.

In [ ]:
# Set the variables a, b and c
a = 3
b = 4
c = 5

# Here is a more compact way to do the same thing
a, b, c = 3, 4, 5

# We now print (a squared plus b squared) and (c squared),
# to see if they are equal.  Remember that the correct 
# syntax for a squared is a ** 2, not a ^ 2.
print(a ** 2 + b ** 2,  c ** 2)

# Here is another way to print the result, with some textual explanation
print(f"a squared plus b squared is {a ** 2 + b ** 2} and c squared is {c ** 2}.")

# We can also ask Python to check if the two values are equal,
# using the == operator.  This will return True or False.
print(a ** 2 + b ** 2 == c ** 2)

As another example of Python syntax, we will calculate $2^i \pmod{11}$ for $0\leq i\lt 30$. You should note that the answer consists of a list of ten integers repeated three times. Can you remember enough number theory to explain this?

In [ ]:
print([(2 ** i) % 11 for i in range(30) ])

ABC

In [ ]: