{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When writing computer commands that you can type, the font will change to `command`. For example, `x=2**3.4/5` is a command that can be typed in.\n", "\n", "When we talk about a general command, or some piece of missing data that you should complete, we will use angle brackets as in `` or ``. For example, `` could mean `\"Southampton\"`.\n", "\n", "When showing actual commands as typed into Python, they will start with `In []:`. This is the notation used by the IPython console. The `` allows you to refer to previous commands more easily. The output associated with that command will start with `Out []:`.\n", "\n", "When displaying code, certain commands will appear in different colours. The colours are not necessary. They highlight different types of command or variable. When using the spyder editor you may find the colours match up and are useful: if not, either ignore them or switch them off." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The console - Python as calculator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start with using Python as a calculator. Look at the *console* in the bottom right part of spyder. Here we can type commands and see a result. Simple arithmetic gives the expected results:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.3039215686274517" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(13.5*2.6-1.4)/10.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want to raise a number to a power, say $2^4$, the notation is `**`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is an issue with division. If we divide an integer by an integer, Python `3.X` will do *real* (floating point) division, so that:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, Python `2.X` will do division in the integers:\n", "\n", "```python\n", "In []: 5/2\n", "Out []: 2\n", "```\n", "\n", "If you are using Python `2.X` and want the division operator to behave in this way, start by using the command" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import division" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you really want to do integer division, the command is `//`:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5//2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Further mathematical commands, even things as simple as $\\log$ or $\\sin$, are not included as part of basic Python:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'log' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlog\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2.3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'log' is not defined" ] } ], "source": [ "log(2.3)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'sin' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1.4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'sin' is not defined" ] } ], "source": [ "sin(1.4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, note the way that errors are reported: we'll see this a lot, and there's useful information there to understand. It's telling us\n", "\n", "1. Where the problem occurred\n", "2. What the problem is\n", "\n", "The language Python uses takes some getting used to, but it's worth the effort to read these messages, and think about what it's trying to say. Here it's pointing to the line where the problem happened, and saying \"I don't understand what this command is!\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Going back to the mathematics, we obviously want to be able to compute more mathematical functions. For this we need a *module* or *package*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Importing modules and packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Anything that isn't provided by the base Python can be provided by modules and packages. A module is a file containing functions and definitions that we can include and use in our code. A package is a collection of modules. They're not included by default, to reduce overhead. They're easy to write - we will write our own later - and easy to include." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use a package we must *import* it. Let's look at the `math` package." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.8329091229351039" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(2.3)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.9320390859672263" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sin(1.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use the package we've typed `import `, where in this case `` is `math`. Then we can use functions from that package by typing `.`, as we have here when `` is either `log` or `sin`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The \"dot\" notation may seem annoying, and can be avoided by specifying what functions and constants we want to use. For example, we could just get the `log` function and use that:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import log" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.8329091229351039" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log(2.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, the \"dot\" notation is useful, as we often find the same symbol or name being used for many different purposes. For example, the `math` package contains the mathematical constant $e$ as `math.e`:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.e" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But there is also the electron charge, usually denoted $e$, which is in the `scipy.constants` package:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import scipy.constants" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.6021766208e-19" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scipy.constants.e" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To avoid these name clashes we can import something *as* a different name:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import e" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from scipy.constants import e as charge_e" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.6021766208e-19" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "charge_e" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will often see this method used to shorten the names of imported modules or functions. For example, standard examples often used are:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The commands can then be used by typing `np.`, or `plt.`, which saves typing. We would encourage you *not* to do this as it can make your code less clear." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A *variable* is an object with a name and a value:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In standard programming all variables must have a value (although that value may be a placeholder to say \"this variable doesn't have a reasonable value *yet*\"). Only symbolic packages can mirror the analytical method of having a variable with no specific value. However, code can be written *as if* the variables had no specific value.\n", "\n", "For example, we cannot write" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'y' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'y' is not defined" ] } ], "source": [ "x = y**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "as `y` does not have a specific value yet. However, we can write" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = 3.14159627" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = y**2" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9.869627123677912\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and get a sensible result, even though we have written the exact same line of code, as now `y` has a specific value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Warning\n", "\n", "Note that we have defined the variable `x` twice in rapid succession: first as an *integer* (`x=2`) and next as a *floating point number*, or *float* (the computer's implementation of a real number, using `x=y**2`, where `y` is a float). Not all programming languages allow you to do this. In a *statically typed* language you have to say whether a variable will be an integer, or a float, or another type, before you define it, and then it cannot change. Python is *dynamically typed*, so any variable can have any type, which can be changed as we go." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A variable is an object with a name, but not just any name will do. Python has rules which *must* be followed, and conventions that *should* be followed, with a few gray areas.\n", "\n", "Variables *must*\n", "\n", "* not contain spaces\n", "* not start with a number\n", "* not contain a special character (such as `!@#$%^&*()\\|`)\n", "\n", "So the following are valid:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "half = 1.0/2.0" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "one_half = 1.0/2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "but the following are not:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m one half = 1.0/2.0\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "one half = 1.0/2.0" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1_half = 1.0/2.0\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "1_half = 1.0/2.0" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m one!half = 1.0/2.0\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "one!half = 1.0/2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables *should*\n", "\n", "* be descriptive, ie say what their purpose is in the code\n", "* be written entirely in lower case\n", "* separate different words in the variable name using underscores\n", "\n", "More detail can be found in [PEP8](https://www.python.org/dev/peps/pep-0008/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables *may* contain some unicode characters, depending on Python version and operating system. In Python `3` you can include accents or extended character sets in variable names:\n", "\n", "```python\n", "rôle = 5\n", "π = math.pi\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, these tricks are not always portable between different Python versions (they aren't guaranteed to work in Python `2`), or different operating systems, or even different machines. To ensure that your code works as widely as possible, and that the methods you use will carry over to other programming languages, it is recommended that variables do not use any extended characters, but only the basic latin characters, numbers, and underscores." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Equality and variable assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One thing that may seem odd, or just plain *wrong* to a mathematician, is two statements like" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 2" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How can `x` equal *both* 2 and 3? Mathematically, this is nonsense." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The point is that, in nearly every programming language, the `=` symbol is not mathematical equality. It is the *assignment* operation: \"set the value of the variable (on the left hand side) equal to the result of the operation (on the right hand side)\". This implies another difference from the mathematical equality: we cannot flip the two sides and the line of code mean the same. For example," ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "can't assign to literal (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 3 = x\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to literal\n" ] } ], "source": [ "3 = x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "immediately fails as `3` is not a variable but a fixed quantity (a *literal*), which cannot be assigned to. Mathematically there is no difference between $x=3$ and $3=x$; in programming there is a huge difference between `x=3` and `3=x` as the meaning of `=` is not the mathematical meaning." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get closer to the standard mathematical equality, Python has the `==` operator. This compares the left and right hand sides and says whether or not their values are equal:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x == 3.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, this may not be exactly what we want. Note that we have assigned `x` to be the *integer* 3, but have compared its value to the *float* 3.0. If we want to check equality of value and type, Python has the `type` function:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x) == type(3.0)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x) == type(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Direct comparisons of equality are often avoided for floating point numbers, due to inherent numerical inaccuracies:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": true }, "outputs": [], "source": [ "p = 2.01" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-8.881784197001252e-16" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p**2-4.0401" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p**2 == 4.0401" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will return to this later." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Debugging" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Making mistakes and fixing them is an essential part of both mathematics and programming. When trying to fix problems in your code this process is called *debugging*. As you've been following along with the code you will have made \"mistakes\" as there are intentionally broken commands above to show, for example, why `one!half` is not a valid variable (and you may have made unintentional mistakes as well)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a number of techniques and strategies to make debugging less painful. There's more detail in later chapters, but for now let's look at the crucial first method." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading the error message\n", "\n", "When you make a mistake Python tells you, and in some detail. When using IPython in particular, there is much information you can get from the error message, and you should read it in detail. Let's look at examples.\n", "\n", "### Syntax error\n", "\n", "A *syntax error* is when Python cannot interpret the command you have entered." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m one half = 1.0/2.0\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "one half = 1.0/2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example we saw above. The `SyntaxError` is because of the space - `one half` is not a valid variable name. The error message says `invalid syntax` because Python can't interpret the command on the left of the equals sign. The use of the carat `^` points to the particular \"variable\" that Python can't understand." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "unexpected EOF while parsing (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m x = 1.0 / ( 2.0 + (3.0 * 4.5)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unexpected EOF while parsing\n" ] } ], "source": [ "x = 1.0 / ( 2.0 + (3.0 * 4.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example is still a `SyntaxError`, but the pointer (`^`) is indicating the end of the line, and the statement is saying something new. In this statement `unexpected EOF while parsing`, \"`EOF`\" stands for \"end of file\". This can be reworded as \"Python was reading your command and got to the end before it expected\".\n", "\n", "This usually means something is missing. In this case a bracket is missing - there are two left brackets but only one right bracket.\n", "\n", "A similar example would be" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "EOL while scanning string literal (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m name = \"This string should end here...\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n" ] } ], "source": [ "name = \"This string should end here..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case the error message includes \"`EOL`\", standing for \"end of line\". So we can reword this message as \"Python was reading your command and got to the end of the line before the string finished\". We fix this by adding a `\"` at the end of the line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, what happens if the error is buried in many lines of code? This isn't the way we'd enter code in the console, but is how we'd deal with code in the notebook, or in a script or file:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 3)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m z = 4.5 * x y + y\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "x = 1.0\n", "y = 2.3\n", "z = 4.5 * x y + y\n", "a = x + y + z\n", "b = 3.4 * a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the error message points to the *specific line* that it can't interpret, *and* it says (at first) which line this is.\n", "\n", "### Name Errors\n", "\n", "This is where Python doesn't know the variable you're trying to refer to:" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'my_variable_3' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmy_variable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmy_variable_2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmy_variable\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mmy_variable_3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'my_variable_3' is not defined" ] } ], "source": [ "my_variable = 6\n", "my_variable_2 = 10\n", "x = my_variable + my_variable_3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It usually means you've made a typo, or have referred to a variable before defining it (sometimes by cutting and pasting code around). Using tab completion is a good way of minimizing these errors.\n", "\n", "Note that there is a difference between syntax errors and other errors. Syntax errors are spotted by the code before it is run; other errors require running the code. This means the format of the output is different. Rather than giving the line number and using a carat (\"`^`\") to point to the error, instead it points to the code line using \"`---->`\" and adds the line number before the line of code.\n", "\n", "### Numerical errors\n", "\n", "A range of \"incorrect\" mathematical operations will give errors. For example" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "float division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1.0\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero" ] } ], "source": [ "1.0/0.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Obviously dividing by zero is a bad thing to try and do within the reals. Note that the definition of floating point numbers *does* include the concept of infinity (via \"`inf`\"), but this is painful to use and should be avoided where possible." ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ValueError", "evalue": "math domain error", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: math domain error" ] } ], "source": [ "math.log(0.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `ValueError` generally means that you're trying to call a function with a value that just doesn't make sense to it: in this case, $\\log(x)$ just can't be evaluated when $x=0$." ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "ename": "OverflowError", "evalue": "(34, 'Result too large')", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m10.0\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10.0\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10.0\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m10.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mOverflowError\u001b[0m: (34, 'Result too large')" ] } ], "source": [ "10.0**(10.0**(10.0**10.0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An `OverflowError` is when the number gets too large to be represented as a floating point number.\n", "\n", "### General procedure\n", "\n", "Having read the error message, find the line in your code that it indicates. If the error is clear, then fix it. If not, try splitting the line into multiple simpler commands and see where the problem lies. If using spyder, enter the commands into the editor and see if the syntax highlighting helps spot errors." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise: Variables and assignment\n", "\n", "## Exercise 1\n", "\n", "Remember that $n! = n \\times (n - 1) \\times \\dots \\times 2 \\times 1$. Compute $15!$, assigning the result to a sensible variable name.\n", "\n", "## Exercise 2\n", "\n", "Using the `math` module, check your result for $15$ factorial. You should explore the help for the `math` library and its functions, using eg tab-completion, the spyder inspector, or online sources.\n", "\n", "## Exercise 3\n", "\n", "[Stirling's approximation](http://mathworld.wolfram.com/StirlingsApproximation.html) gives that, for large enough $n$, \n", "\n", "$$ n! \\simeq S = \\sqrt{2 \\pi} n^{n + 1/2} e^{-n}. $$\n", "\n", "Using functions and constants from the `math` library, compare the results of $n!$ and Stirling's approximation for $n = 5, 10, 15, 20$. In what sense does the approximation improve (investigate the absolute error $|n! - S|$ and the relative error $|n! - S| / n!$)?" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" }, "nbconvert": { "title": "Python Basics" } }, "nbformat": 4, "nbformat_minor": 0 }