Programming is about getting the computer to do the calculation for you. This is needed when the calculation is long and has many repetitive steps. It does not mean that you can get the computer to understand things for you: usually you need to understand the steps before telling the computer what to do!
Using a computer, particularly for mathematical or scientific purposes, involves a lot more than programming. There is also
First, we will get the computer to do something, and later worry about doing it efficiently and effectively. Your time is more valuable than the computer's (literally: compare the hourly cost of computer time through eg Amazon, typically much less than $5 per hour, against the minimum wage). We want the computer doing the work, and only when that wastes your time should you worry about the speed of the calculation.
The four essential sections are on the basics, programs, loops and flow control, and basic plotting. You should work through the notes by typing in the commands as they appear, ensuring that you understand what's going on, and seeing where you make mistakes. At the end of each section, try the exercises that you think you can tackle. Also look back at previous exercises and see if you can solve them more straightforwardly with your additional knowledge.
The section on classes should be read before reading the other sections: the details of creating your own classes won't be needed for later sections, but some understanding is important. The section on scientific Python is then the most important and should be explored in detail. At this point you should be able to tackle most of the exercises.
The sections on symbolic Python and statistics should then be covered to get an overview of how Python can be used in these areas. The section on LaTeX is not directly related to programming but is essential for writing mathematical documents. Further sections are useful as your codes get more complex, but initially are less important.
When working on code it is often very useful to work in pairs, or groups. Talk about what you're doing, and why you're doing it. When something goes wrong, check with other people, or explain to them what you're trying to do (rubber duck debugging). When working on exercises, use pair programming techniques. If there's more than one way of doing something, try them all and see which you think is best, and discuss why.
There is no "one right way" to code, but well documented, easy to understand, clearly written code that someone else can follow as well is always a good start.
To introduce programming we will use the Python programming language. It's a good general purpose language with lots of tools and libraries available, and it's free. It's a solid choice for learning programming, and for testing new code.
A number of Python tools are available on a standard university desktop machine. We will mostly be using Python through spyder, which allows us to write, run, test and debug python code in one place. To launch spyder, either type spyder in the search bar, or go to Start, then All Programs, then Programming Languages, then Anaconda, then choose spyder.
As Python is free you can install and run it on any machine (or tablet, or phone) you like. In fact, many will have Python already installed, for the use of other software. However, for programming, it is best to have an installation that all works together, which you can easily experiment with, and which won't break other programs if you change something. For these reasons, we recommend you install the anaconda distribution.
First, download the miniconda package for your computer. There are two versions of Python: a Python 2.X and a Python 3.X. There are small differences between the two. Everything we show here will work on either version. We will be using the 3.X version.
The miniconda package installs the basic Python and little else. There are a number of useful packages that we will use. You can install those using the conda app (either via the launcher, or via the command line). But before doing that, it is best to create an environment to install them in, which you can modify without causing problems.
Packages may rely on other packages, and may rely on specific versions of other packages in order to work. This can lead to "dependency hell", when you need (for different purposes) package A and package B which rely on conflicting versions of package C.
The answer to this is environments, which allow you to organize your different packages to minimize conflicts. Environments are like folders, and you have one for each project you are working on. That way, you ensure that updating or installing packages for one project does not cause problems for a different project.
We will create a single environment called labs. If you are running on a Mac or on Linux, open a terminal. If on Windows, use the Anaconda command prompt. Then type
conda create -n labs python=3
This creates the new environment, and installs the basic python package in the python 3.X flavour. It does not activate the environment. In order to work within this environment, if using Windows type
activate labs
If using Mac or Linux type
source activate labs
Then any command launched from the terminal or command prompt will use the packages in this environment.
After creating the environment, and activating it, the key packages that need installing are:
ipythonnumpymatplotlibscipyspyderspyder-appsympyOther packages that will be useful are
jupyternosenumbapandasThe command to install new packages is conda install. So, to install the packages above type (or copy and paste) first
activate labs
if on Windows, or
source activate labs
if on Mac or Linux, and then type
conda install ipython numpy matplotlib scipy spyder spyder-app sympy \
jupyter nose numba pandas
Note: the '\' backslash character should continue an overly long line: if you are typing and not copying and pasting this should be unnecessary.
This will download and install a lot of additional packages that are needed; just agree and continue.
There are many ways of writing and running Python code. If you open a terminal (on Mac or Linux; on Windows, this would be an Anaconda terminal or a Command Window) you can type python or ipython to launch a very bare bones console. This allows you to enter code which it will then run.
More helpful alternatives are Integrated Development Environments (IDEs) or the notebook. The Jupyter notebook (previously called the IPython notebook) is browser based and very powerful for exploratory computing. To run, type jupyter notebook in the terminal prompt.
However, when just getting started, or when writing large codes, IDEs are a better alternative. A simple IDE is spyder which we will use here. To launch, either select spyder from the appropriate menu, or type spyder at the terminal prompt.
You should then see a screen something like the figure (without the annotations).
The four essential parts of the screen are outlined.
spyder looks. You should ensure that the working directory is set to the location where the file is.For further information on using and setting up spyder, see this tutorial.
A crucial feature of IPython and spyder that saves time and reduces errors is tab completion. When typing anything, try pressing the tab key. This will either automatically complete the name of the variable (or function, or class), or will present a list of options. This is one way of finding out what functions are available - press tab and it will list them all! By typing the first few characters and then pressing tab, you can rapidly narrow down the options.
There are many ways of getting help. The most useful are:
help(<thing>). Works in the console.<thing>? or <thing>??. Works in the console.There's a lot of material related to Python online and in the library. None are essential, but lots may be useful. The particular books recommended as a first look are
These notes have been constructed using the following versions of Python and related packages:
In [1]:
%load_ext watermark
%watermark -v -m -g -p numpy,scipy,matplotlib,sympy