Pages

IMA Workshop (online): Python for A-Level Mathematics and Beyond

On Friday 21st January 2022 I attended an online training course hosted by the Institute of Mathematics and its Applications (IMA) and delivered by Dr Stephen Lynch. The course was titled Python for A-Level Mathematics and Beyond, and was marketed at delegates with "no prior knowledge of programming".

At the beginning of the workshop I started to live-tweet...

... but very soon needed to concentrate too much to be able to continue. As a handful people seemed to be interested in the content I thought I'd write up my experience here.

In the beginning

I first saw the course whilst rummaging through the IMA's event listings in December last year. Alas, I was too late to book onto it, but noticed that it seems to be a semi-regular offering, being available in January, which I promptly booked and paid for (£5 for IMA members, if you're interested).

A few days before the course we received the obligatory Zoom link and joining information, which was accompanied by a quick list of requirements, which including downloading and installing the Anaconda software package. I'm not sure how to explain what Anaconda is, other than being something of a portal to some environments within which you can do coding.

This was a very promising start for someone of my level of experience: I've tried taking part in Python training courses before, and whilst all of them have made similar claims to this one of requiring "no prior knowledge of coding", the vast majority have started from the point at which you start tapping away on your keyboard (i.e., no mention of where you should be typing). Receiving this information reassured me that the course designers realised what an actual coding beginner needed! I wouldn't call myself a complete coding virgin as I understand the general idea behind coding (I mean, it seems to me that different coding languages are just different sets of rules for typing out maths in such a way that your computer understands what you want it to do), but this first bit, the actual where the hell do I type bit, is the part I've struggled to get past, and is the bit that's often missed off.

From the listing I had assumed that the course was aimed at teachers, but it became apparent during the day that there were a mixture of teachers and students present. From the comments appearing in the chat (we were encouraged to interact with each other as well as to ask questions) it was evident that I was neither the least 

Structure

The course ran from 12 - 5pm, split up into four sessions separated by two coffee and one lunch break:
12:00 - 13:00: Introduction & Using Python as a Powerful Graphing Calculator
13:15 - 14:00: Symbolic Python with Sympy
14:30 - 15:30: Numerical Python and Plots Using Numpy and Matplotlib
15:45 - 17:00: Programming, Jupyter Notebooks, Google Colab & Beyond A-Level Maths

I'll summarise each of these sessions below:

1. Introduction & Using Python as a Powerful Graphing Calculator

We were introduced to Python and told what it was: "a high-level computing language [that] supports imperative, object-oriented, and procedural styles of programming". This included some of its advantages, as well as some of the advantages of programming generally, including opportunities to investigate and therefore understand mathematics more deeply. Then we found out:

1.1 How to access Python.

Yes! Finally!
Dr Lynch checked that we'd all managed to download and install Anaconda and instructed us to click the button within Anaconda to launch Spyder (Scientific Python Development Environment), which is an Integrated Development Environment (IDE), which basically means that it's a programme for writing and running Python scripts and programs. He then explained the various windows that opened and what they were used for.

1.2 How to use the Spyder console window as a powerful calculator

The last half hour of the first session was a relatively quickfire progression through Python's mathematical functions, from the built-in syntax for adding, subtracting, multiplying, dividing and calculating powers, to learning how to import more powerful maths tools from the math library (import math, dontcha know, followed by help(math) to list all the functions. from math import * imports all of them, though similar syntax allows you to import them individually if you're writing a specific program), and to use them to perform more complicated calculations. These began with computing factorials, progressed through logarithms, trig functions, fractions; the floor, ceiling and truncation functions; modular arithmetic; and calling constants such as $e$, $\pi$ and $\tau$.
This finished with how to quit the console and restart. Another thing that's massively important but often forgotten from training.

2. Symbolic Python with SymPy

SymPy (Symbolic Python) is another library that allows Python to be used to perform symbolic maths (i.e. algebra). Telling Python you want to use that library is easy (type from sympy import * to import everything from SymPy). Using this library you can declare the symbols you want to use (e.g. x, y = symbols(' x y ')) and then do some maths with them. Again, we ran through a page of examples showing us how to do such things as factorise $x^2 - y^2$ (factor(x**2 – y**2)), solve an algebraic equation (solve(x**2 – 4*x – 3, x)), simplify trigonometric expressions, define and calculate limits, differentiate (including multiple times), integrate (including indefinite and definite), define and multiply matrices, calculate determinants and evaluate constants (such as $\pi$) to so many decimal places.

3. Numerical Python and Plots Using Numpy and Matplotlib

3.1 NumPy

In the first part of this session we learned about the NumPy (Numerical Python) library (import numpy) which allows Python to compute with lists, arrays, vectors and tensors. This included defining and generating, adding to and deleting from, extracting information from and calculating with lists and arrays.

3.2 MatPlotLib

The second part of session 3 took us beyond the Console window, and into the Editor window, which is where programs are written. Using the pyplot function in the matplotlib library (import matplotlib.pyplot) we found out how to get Python to plot graphs, which required multi-line programs (that's a key difference between the Console and Editor windows: the first allows you to run one function or command at a time; the latter enables the construction of more detailed programs that utilise multiple functions feeding into one another). Specific examples included a parabola, a parametric plot, and plotting two trigonometric curves on one set of axes.

4. Programming, Jupyter Notebooks, Google Colab & Beyond A-Level Maths

4.1 Programming

The first part of session 4 stayed with the Editor window with an introduction to programming. We began by defining our own function that returned the square of a number; and then we created a program to convert from Farenheit to Kelvin. We were introduced to while loops, creating a program that calculated the sum of all natural numbers up to $n$, and then produced a program to list the first $\n$ terms of the Fibonacci sequence. We then extended these ideas with a program that tested an integer input to see whether it is positive, negative or zero.

4.2 Jupyter Notebooks

Part 2 of session 4 introduced us to Jupyter notebooks (accessed by clicking the appropriate launch button on Anaconda's main screen). Again, I'm not entirely sure how to describe one, but my best attempt is that a Jupyter notebook is a document in which different programming languages (or just text) can be used to different cells (laid out vertically like paragraphs) on the page: we used $\LaTeX$ to write a title and a heading for the page in one cell, and then write a Python script to plot a projectile graph in the second cell.
A very interesting feature of Jupyter notebooks is that they can be exported in a number of ways, including as a HTML file, which means they can be uploaded as a page on a website. This seemed to have many possibilities regarding teaching A-Level students, not least that you could share the document with them with ease, and they would be able to see not only the results of the code but also the code itself.
Jupyter notebooks (if I understand things correctly) run via software on your computer which uses your web browser to display and manipulate them. This means that whilst they look like they're on the internet they're actually stored on your computer, which is why they need to be exported to be shared.

4.3 Google Colab

Google's Colab appears to be very similar to Jupyter notebooks (actually, I think it's the case that Jupyter notebooks themselves can be created and edited within Google Colab), but as with everything Google, it's all in the cloud. This means that your programs are not limited to your own computer's resources, but I understand that there are limits on how many of Google's cloud resources you can use with a free account. In terms of storage, Google Colab projects are stored in Google Drive and count towards whatever storage you have available there.
We used Google Colab to create an animated plot of the function $y=\sin(0.05xn)$ for $n$ in the range 1 - 100.

Beyond the Course

We were left with a number of resources that will enable us to continue practising and learning. These include:
  • Full workshop notes including exercises related to each section
  • Links to resources hosted at Github and as a Jupyter notebook produced to accompany Dr Lynch's book, Dynamical Systems with Applications using Python (as I found the course so useful I don't begrudge him the opportunity to plug a book!)
  • A Jupyter notebook accompanying the course that lists examples of simple (for some values of "simple", of course...) Python commands and programs that can be used to help with the understanding of A-Level maths. There are around 50 such examples in total, including full program code, listed under the 42 AS and A-Level units from the MEI schemes of work.
In all, I found it a fantastically helpful course that was perfectly pitched to my level of experience with Python and programming in general. I finished the day inspired to explore how Python might help me to not only update my teaching practice, but also to expand my own understanding of mathematics.
Have you used or developed any resources utilising the power of Python to improve or enhance your teaching of mathematics at any level, or to aid teachers? Please let me know either in the comments or by contacting me directly!

1 comment:

Hi, thanks for commenting. If you feel passionately about anything I've posted, please feel free to make your views known but please take the time to make sure that your comments are rational, considered and suitable for any audience.

Thanks for reading!