An Introduction to Python for Scientific Computing
Total Page:16
File Type:pdf, Size:1020Kb
An introduction to Python for scientific computing Table of contents Table of contents ............................................................................................................................ 1 Overview ......................................................................................................................................... 3 Installation ...................................................................................................................................... 3 Other resources .............................................................................................................................. 4 Interactive interpreter .................................................................................................................... 4 Everything is an object .................................................................................................................... 6 Basic types ....................................................................................................................................... 7 Python as a calculator ..................................................................................................................... 8 Boolean values and comparison operators .................................................................................... 9 Variable assignment ...................................................................................................................... 10 Strings ........................................................................................................................................... 10 Special characters in strings .......................................................................................................... 11 String formatting ........................................................................................................................... 12 Lists ............................................................................................................................................... 14 Accessing list elements ................................................................................................................. 16 List comprehensions ..................................................................................................................... 17 List operations and functions........................................................................................................ 18 Tuples and immutable versus mutable objects ............................................................................ 21 Assignment and name binding ..................................................................................................... 22 Multiple assignment ..................................................................................................................... 25 String functions and manipulation ............................................................................................... 27 Dictionaries ................................................................................................................................... 29 If statements ................................................................................................................................. 31 For loops ....................................................................................................................................... 32 While loops ................................................................................................................................... 36 Functions ....................................................................................................................................... 37 © 2019 M. Scott Shell 1/62 last modified 9/24/2019 Optional arguments in functions .................................................................................................. 39 Function namespaces ................................................................................................................... 40 Functions as objects ...................................................................................................................... 42 Function documentation .............................................................................................................. 42 Writing scripts ............................................................................................................................... 43 Modules ........................................................................................................................................ 44 Standard modules ......................................................................................................................... 46 Reading from files ......................................................................................................................... 47 Writing to files............................................................................................................................... 51 Binary data and compressed files ................................................................................................. 52 File system functions .................................................................................................................... 53 Command line arguments ............................................................................................................. 55 Classes ........................................................................................................................................... 56 Exceptions ..................................................................................................................................... 58 Timing functions and programs .................................................................................................... 60 © 2019 M. Scott Shell 2/62 last modified 9/24/2019 Overview Python is an extremely usable, high-level programming language that is now a standard in scientific computing. It is open source, completely standardized across different platforms (Windows / MacOS / Linux), immensely flexible, and easy to use and learn. Programs written in Python are highly readable and often much shorter than comparable programs written in other languages like C or Fortran. Moreover, Python comes pre-loaded with standard modules that provide a huge array of functions and algorithms, for tasks like parsing text data, manipulating and finding files on disk, reading/writing compressed files, and downloading data from web servers. Python is also capable of all of the complex techniques that advanced programmers expect, like object orientation. Python is somewhat different than languages like C, C++, or Fortran. In the latter, source code must first be compiled to an executable format before it can be run. In Python, there is no compilation step; instead, source code is interpreted on the fly in a line-by-line basis. That is, Python executes code as if it were a script. The main advantage of an interpreted language is that it is flexible; variables do not need to be declared ahead of time, and the program can adapt on-the-fly. The main disadvantage, however, is that numerically-intensive programs written in Python typically run slower than those in compiled languages. This would seem to make Python a poor choice for scientific computing; however, time-intensive subroutines can be compiled in C or Fortran and imported into Python in such a manner that they appear to behave just like normal Python functions. Fortunately, many common mathematical and numerical routines have been pre-compiled to run very fast and grouped into two packages that can be added to Python in an entirely transparent manner. The NumPy (Numeric Python) package provides basic routines for manipulating large arrays and matrices of numeric data. The SciPy (Scientific Python) package extends the functionality of NumPy with a substantial collection of useful algorithms, like minimization, Fourier transformation, regression, and other applied mathematical techniques. Both of these packages are also open source and growing in popularity in the scientific community. With NumPy and SciPy, Python become comparable to, perhaps even more competitive than, expensive commercial packages like MatLab. This tutorial will cover the Python 2.7 language version. A newer language version, the 3.0 series, also exists, but breaks compatibility with the earlier versions of the language. The 2.7 series is still widely used for scientific computing efforts in Python. Installation To use Python, one must install the base interpreter. In addition, there are a number of applications that provide a nice GUI-driven editor for writing Python programs. For Windows © 2019 M. Scott Shell 3/62 last modified 9/24/2019 platforms, I prefer to use the freely available Anaconda installation and the included Spyder editor. This single package includes virtually all tools one would need for scientific Python computing, and can be downloaded at: https://www.anaconda.com/distribution/ Download the installation executable and proceed through the automated setup. Most of the modules that you will need are pre-installed. Other resources Python comes standard with extensive documentation. The entire manual, and many other helpful documents and links, can also be found at: http://docs.python.org The Python development community also maintains an extensive wiki. In particular, for