Transcription of Maths with Python Documentation - Read the Docs
1 Maths with Python DocumentationRelease Sciences, University of SouthamptonOct 13, 2016 Contents1 First steps .. to use these notes .. list ..82 Python .. : Variables and assignment ..173 .. programs and modules .. and strings .. it together .. : basic functions .. : Floating point numbers ..314 Loops - how to repeat .. and Sequences .. flow .. : Prime numbers ..465 Basic .. : Logistic map ..536 Classes and and Object Oriented Programming .. : Equivalence classes ..597 Scientific Python .. : Lorenz attractor .. : Mandelbrot .. : The shortest published Mathematical paper ..848 Symbolic Python .. reading .. : systematic ODE solving ..979 .. data in .. statistical functions .. data .. numbers .. : Anscombe s quartet .. 10910 Exceptions and Exceptions and Testing.
2 Exceptions .. Testing .. 11711 Iterators and Iterators and Generators .. Exercise : twin primes .. Exercise : a basis for the polynomials .. 13112 Classes and Classes and Object Oriented Programming .. Exercise: Equivalence classes .. Exercise: Rational numbers .. 13913 Indices and tables141iiMaths with Python Documentation , Release is material for an introductory Python course for first year undergraduate Mathematics students at the Universityof with Python Documentation , Release First stepsProgramming is about getting the computer to do the calculation for you. This is needed when the calculation is longand has many repetitive steps. It doesnotmean that you can get the computer to understand things for you: usuallyyou need to understand the steps before telling the computer what to do!
3 Using a computer, particularly for mathematical or scientific purposes, involves a lot more than programming. Thereis also Algorithmic thinking: understanding how to convert the solution to a problem into a sequence of steps that canbe followed without further explanation. Efficient implementationandcomplexity: there are many ways to solve a given problem, which will give equiv-alent answers in principle. In reality, some solutions will solve some problems to a reasonable accuracy inreasonable time, and it can be important to be able to check which solutions work in which cases. Effective implementation: solving a problem on a computeronceis great. Being able to re-use your solution onmany problems is much better. Being able to give your code to anybody else, and it working for them, or sayingwhyit won t work, without further input from you, is best. Reproducible science: in principle, any scientific result should be able to be checked by somebody else.
4 Withcomplex scientific code, presenting and communicating its contents so that others can reproduce results is im-portant and not always , we will get the computer to dosomething, and later worry about doing it efficiently and effectively. Your time ismore valuable than the computer s (literally: compare the hourly cost of computer time through eg Amazon, typicallymuchless than $5 per hour, against the minimum wage). We want the computer doing the work, and only when thatwastes your time should you worry about the speed of the How to use these The materialThe four essential sections are on the basics, programs, loops and flow control, and basic plotting. You should workthrough the notes by typing in the commands as they appear, ensuring that you understand what s going on, and seeingwhere you make mistakes. At the end of each section, try the exercises that you think you can tackle.
5 Also look backat previous exercises and see if you can solve them more straightforwardly with your additional with Python Documentation , Release section on classes should be read before reading the other sections: the details of creating your own classes won tbe needed for later sections, but some understanding is important. The section on scientific Python is then the mostimportant and should be explored in detail. At this point you should be able to tackle most of the sections on symbolic Python and statistics should then be covered to get an overview of how Python can be usedin these areas. The section on LaTeX is not directly related to programming but is essential for writing mathematicaldocuments. Further sections are useful as your codes get more complex, but initially are less How to work when codingWhen working on code it is often very useful to work in pairs, or groups.
6 Talk about what you re doing, and whyyou 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 oneway of doing something, try them all and see which you think is best, and discuss is no one right way to code, but well documented, easy to understand, clearly written code that someone elsecan follow as well is always a good PythonTo introduce programming we will use the Python programming language. It s a good general purpose language withlots of tools and libraries available, and it s free. It s a solid choice for learning programming, and for testing Using Python on University machinesA number of Python tools are available on a standard university desktop machine. We will mostly be using Pythonthrough spyder, which allows us to write, run, test and debug Python code in one place.
7 To launch spyder, eithertypespyderin the search bar, or go toStart, thenAll Programs, thenProgramming Languages, thenAnaconda, then Using Python on your own machineAs Python is free you can install and run it on any machine (or tablet, or phone) you like. In fact, many will havePython already installed, for the use of other software. However, for programming, it is best to have an installationthat all works together, which you can easily experiment with, and which won t break other programs if you changesomething. For these reasons, we recommend you install the anaconda you have enough bandwidth and time (you will be downloading about 1G of software) then you can use the Ana-conda graphical installer. There are two versions of Python : a a There are smalldifferences between the two. Everything we show here will work on either version.
8 We will be using Anaconda package installs both the essential Python package and a large amount of useful Python software. It willput a launcher icon on your desktop. Clicking on the launcher will bring up a window listing a number of applications:we will be usingspyderas seen 1. First StepsMaths with Python Documentation , Release you do not want to download all the Python packages, but only the essential ones, there is a smaller version ofAnaconda, called miniconda. First, download the miniconda package for your computer. Again, we will be using miniconda package installs the basic Python and little else. There are a number of useful packages that we willuse. You can install those using thecondaapp (either via the launcher, or via the command line). But before doingthat, it is best to create an environment to install them in, which you can modify without causing may rely on other packages, and may rely onspecific versionsof other packages in order to work.
9 This canlead to dependency hell , when you need (for different purposes) packageAand packageBwhich rely on conflictingversions of answer to this isenvironments, which allow you to organize your different packages to minimize conflicts. Envi-ronments are like folders, and you have one for each project you are working on. That way, you ensure that updatingor installing packages for one project does not cause problems for a different get the most out of environments, we need to use the command line, or , or a *command prompt window*, is a window where commands can be typed in to directly run commandsor affect files. On Windows you select theCommand Promptfrom the Accessories menu. On Mac or Linux systemyou open a terminal or an XTerm. Inside the terminal you can change directories using thecdcommand, and runcommands associated with Anaconda or miniconda using the environmentWe will create a single environment calledlabs.
10 If you are running on a Mac or on Linux, open a terminal. If onWindows, use a command prompt. Then typeconda create -n labs Python =3 This creates the new environment, and installs the basicpythonpackage in thepython It does notactivate the environment. In order to work within this environment, if using Windows typeactivate labsIf using Mac or Linux typesource activate labsThen any command launched from the terminal or command prompt will use the packages in this creating the environment, and activating it, the key packages that need installing (if using miniconda; they areall installed with the full Anaconda) are: Python5 Maths with Python Documentation , Release numpy matplotlib scipy spyder spyder-app sympyOther packages that will be useful are jupyter nose numba pandasThe command to install new packages isconda install. So, to install the packages above type (or copy andpaste) firstactivate labsif on Windows, orsource activate labsif on Mac or Linux, and then typeconda install ipython numpy matplotlib scipy spyder spyder-app sympy\jupyter nose numba pandasNote: the \ backslash character should continue an overly long line: if you are typing and not copying and pastingthis should be will download and install a lot of additional packages that are needed; just agree and SpyderThere are many ways of writing and running Python code.