An introduction to Python for scientific computing
Table of contents
Table of contents 1
Overview 3
Installation 3
Other resources 4
Interactive interpreter 5
Everything is an object 7
Basic types 7
Python as a calculator 8
Boolean values and comparison operators 9
Variable assignment 10
Strings 11
Special characters in strings 12
String formatting 12
Lists 15
Accessing list elements 16
List comprehensions 18
List operations and functions 19
Tuples and immutable versus mutable objects 21
Assignment and name binding 22
Multiple assignment 26
String functions and manipulation 27
Dictionaries 29
If statements 31
For loops 33
While loops 37
Functions 38
© 2014 M. Scott Shell 1/63 last modified 6/17/2014
Optional arguments in functions 40
Function namespaces 41
Functions as objects 42
Function documentation 43
Writing scripts 44
Modules 45
Standard modules 47
Reading from files 48
Writing to files 51
Binary data and compressed files 52
File system functions 54
Command line arguments 56
Classes 57
Exceptions 59
Timing functions and programs 61
© 2014 M. Scott Shell 2/63 last modified 6/17/2014
Overview
Python is an extremely usable, high-level programming language that is quickly becoming 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 fu