# Python Programming

Objectives:
* familiarize with the Python programming language and command-line interface
** use correct control structures
** write simple Python programs with correct syntax
* import and use libraries
Resources:
* Introduction to Python for Computational Science and Engineering (A beginner’s guide)
* Google's Python class for developers
* The Beginner's Python Tutorial
* The Official Python Tutorial
* The Official Python for Beginners
* Introduction to Python
* IPython
Python is a scripting language. You can use the interactive Python console to write and execute code. After you log into a Unix/Linux system you can type the type the word "python" (with the quotes) to start the console and hit ^D (ctrl+D) will exit the console. This kind of interactivity makes it ideal for exploratory discovery - finding a solution by exploring and experimentation.

## REPL

The Python console executes a Read-Eval-Print Loop (REPL), which means it prompts you for a expression/command and once you hit enter it will read your expression, evaluate it, print the value of the expression, and prompt you for the next expression. This makes it very easy to test a Python statement. You can use it as a calculator:

## Import Libraries

Many useful functions have been implemented in libraries. To use the functions you need to import them so that their names are recognized. The dir function prints a directory of objects (functions) available in a module:
The help function can be used to find the meaning and the usage of an object from a library as shown in the previous code example. Note that you need to hit the letter Q to quit the help session to return to the Python console.

## Data Types

Python is not a strongly typed language, which means you do not need to declare a specific data type for a variable. But data items associated with variables have types, which are implied - derived from the expression or the operation on the data items. Therefore the data type of a variable can change over time. The type function will tell you the type of data stored in a variable.
There are functions that convert between data types, e.g. int, float, str, and list.
Data types get promoted automatically as necessary.
In Python strings, lists and tuples are sequences. They can be indexed and sliced in the same way. A list in Python can contain data items of different type.

## Source Files

In addition to write and running Python code directly in the console you can save your commands/statements in a file and run the whole file as a script using the Python interpreter. For example, in a shell command-line you can type the following to run your program/script called hello.py:
Indentation matters in Python because it is used for defining scopes, which eliminates the use of braces. Please make sure the code at the same level are indented the same amount represented by tab or space characters.

## Formatted Printing

You can print numbers using a format string similar to the printf function in C.
This example show the limit of the floating point number representation. 1/10 can not be represented precisely because it is not a power of two. Even though the console prints a as 0.1 the underlying representation is almost 0.1.

## Numerical Computation

The following example will run forever till the result overflows the registers because x will never become exactly 1.0 because the representation of 0.1 is an approximation (with an error).
The moral of the lesson is: do not compare floating point numbers for strict equality. Alternatively we can calculate the distance between the two numbers and stop when the distance is short enough (less than a threshold).
If we know exactly how many iterations to perform we can use a for loop as shown below. Note that range(1, 11) => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
What does the following piece of code compute? Will the loop ever finish? Why?
The value eps has will always be a power of 2, therefore there will not be any rounding off errors. However, as the while loop continues eps gets smaller and smaller. Eventually eps become so small it becomes nothing comparing to 1.0, which means adding it to 1.0 will result in 1.0. Recall that the machine epsilon is the smallest value when added to 1.0 will result in value different than 1.0. If a number is less than the machine epsilon and it will cause no effect when it is added to 1.0. sys.float_info tells us that the machine epsilon is 2.220446049250313e-16, which is slightly less than 2x1.11022302463e-16=2.22044604926e-16. This is why when the loop ends eps equals 1.11022302463e-16.

## Symbolic Computation

The SymPy library for Python allows us to manipulate variables symbolically (as symbols). The following code snippets demonstrates some basic functionality of SymPy.

## Numpy Array and Linear Algebra

Sources:
* Numpy Tutorial
* Scipy Tutorial
A array in numpy is multidimensional table of elements of the same type. The elements are indexed by a tuple of positive integers. The name of the class for numpy arrays is ndarray, a.k.a array. Each array object has a number of attributes:
* mdim: number of dimensions
* shape: a tuple of integers indicating the size of the array in each dimension. A matrix with m rows and n columns will have (m, n) as its shape.
* size: the total number elements in an array.
* dtype: the type of elements in an array.
You can install numpy and scipy on Ubuntu as follows:
The elements in an array can be referenced using different syntax as shown in the examples below.
A numpy array can be created from a regular Python list or tuple using the array function. The type of the array elements depends on that of the elements in the sequences.
Numpy includes functions that create arrays with default values. The eye function creates a identity matrix and the identity performs a similar function. The copy function clones an array object includes the data it contains.
Arithmetic operations on numpy arrays are element-wise operations. The product * operator does a element-wise multiplication. The dot function performs matrix multiplications.
Numpy also includes a number of basic linear algebra functions.

## IPython Notebook

The IPython Notebook is an interactive environment that allows coding, executing, documentation of your Python commands.
Resources:
* How to share an IPython notebook
* A gallery of IPython notebooks
* A matplotlib tutorial
* Exploratory computing with Python