Introduction to Python programming

J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/

The latest version of this IPython notebook lecture is available at http://github.com/btabibian/scientific-python-lectures.

The other notebooks in this lecture series are indexed at http://btabibian.github.io/notebooks/learnpython/.

Python program files

• Python code is usually stored in text files with the file ending “.py”:

  myprogram.py

• Every line in a Python program file is assumed to be a Python statement, or part thereof.

• The only exception is comment lines, which start with the character # (optionally preceded by an arbitrary number of white-space characters, i.e., tabs or spaces). Comment lines are usually ignored by the Python interpreter.
• To run our Python program from the command line we use:

  $python myprogram.py  • On UNIX systems it is common to define the path to the interpreter on the first line of the program (note that this is a comment line as far as the Python interpreter is concerned):  #!/usr/bin/env python  If we do, and if we additionally set the file script to be executable, we can run the program like this: $ myprogram.py


Example:

[0m[93mscripts/hello-world-in-swedish.py[0m  [93mscripts/hello-world.py[0m

#!/usr/bin/env python

print("Hello world!")

Hello world!


Character encoding

The standard character encoding is ASCII, but we can use any other encoding, for example UTF-8. To specify that UTF-8 is used we include the special line

# -*- coding: UTF-8 -*-


at the top of the file.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

print("Hej världen!")

Hej världen!


Other than these two optional lines in the beginning of a Python code file, no additional code is required for initializing a program.

IPython notebooks

This file - an IPython notebook - does not follow the standard pattern with Python code in a text file. Instead, an IPython notebook is stored as a file in the JSON format. The advantage is that we can mix formatted text, Python code and code output. It requires the IPython notebook server to run it though, and therefore isn’t a stand-alone Python program as described above. Other than that, there is no difference between the Python code that goes into a program file or an IPython notebook.

Modules

Most of the functionality in Python is provided by modules. The Python Standard Library is a large collection of modules that provides cross-platform implementations of common facilities such as access to the operating system, file I/O, string management, network communication, and much more.

References

• The Python Language Reference: http://docs.python.org/2/reference/index.html
• The Python Standard Library: http://docs.python.org/2/library/

To use a module in a Python program it first has to be imported. A module can be imported using the import statement. For example, to import the module math, which contains many standard mathematical functions, we can do:

This includes the whole module and makes it available for use later in the program. For example, we can do:

1.0


Alternatively, we can chose to import all symbols (functions and variables) in a module to the current namespace (so that we don’t need to use the prefix “math.” every time we use something from the math module:

1.0


This pattern can be very convenient, but in large programs that include many modules it is often a good idea to keep the symbols from each module in their own namespaces, by using the import math pattern. This would elminate potentially confusing problems with name space collisions.

As a third alternative, we can chose to import only a few selected symbols from a module by explicitly listing which ones we want to import instead of using the wildcard character *:

1.0


Finally, one standard practice in the scientific community using Python is to shorten namespaces. This usually works my specifiying two characters or three characters nicknames.

In many code examples you may find on Internet, the author is using these de facto standard nicknames.

Looking at what a module contains, and its documentation

Once a module is imported, we can list the symbols it provides using the dir function:

['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']


And using the function help we can get a description of each function (almost .. not all functions have docstrings, as they are technically called, but the vast majority of functions are documented this way).

Help on built-in function log in module math:

log(...)
log(x[, base])

Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.

2.302585092994046

3.3219280948873626


We can also use the help function directly on modules: Try

help(math)


Some very useful modules form the Python standard library are os, sys, math, shutil, re, subprocess, multiprocessing, threading.

A complete lists of standard modules for Python 2 and Python 3 are available at http://docs.python.org/2/library/ and http://docs.python.org/3/library/, respectively.

Variables and types

Symbol names

Variable names in Python can contain alphanumerical characters a-z, A-Z, 0-9 and some special characters such as _. Normal variable names must start with a letter.

In addition, there are a number of Python keywords that cannot be used as variable names. These keywords are:

and, as, assert, break, class, continue, def, del, elif, else, except,
exec, finally, for, from, global, if, import, in, is, lambda, not, or,
pass, print, raise, return, try, while, with, yield


Note: Be aware of the keyword lambda, which could easily be a natural variable name in a scientific program. But being a keyword, it cannot be used as a variable name.

Assignment

The assignment operator in Python is =. Python is a dynamically typed language, so we do not need to specify the type of a variable when we create one.

Assigning a value to a new variable creates the variable:

Although not explicitly specified, a variable do have a type associated with it. The type is derived form the value it was assigned.

float


If we assign a new value to a variable, its type can change.

int


If we try to use a variable that has not yet been defined we get an NameError:

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-19-36b2093251cd> in <module>()
----> 1 print(y)

NameError: name 'y' is not defined


Fundamental types

int

float

bool

complex

(1-1j)

(1.0, -1.0)


Type utility functions

The module types contains a number of type name definitions that can be used to test if variables are of certain types:

['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'InstanceType', 'IntType', 'LambdaType', 'ListType', 'LongType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType', 'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 'TupleType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType', '__builtins__', '__doc__', '__file__', '__name__', '__package__']

True

False


We can also use the isinstance method for testing types of variables:

True


Type casting

(1.5, <type 'float'>)

(1, <type 'int'>)

((1+0j), <type 'complex'>)

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-33-e719cc7b3e96> in <module>()
----> 1 x = float(z)

TypeError: can't convert complex to float


Complex variables cannot be cast to floats or integers. We need to use z.real or z.imag to extract the part of the complex number we want:

(1.0, ' -> ', True, <type 'bool'>)
(0.0, ' -> ', False, <type 'bool'>)


Operators and comparisons

Most operators and comparisons in Python work as one would expect:

• Arithmetic operators +, -, *, /, // (integer division), ‘**’ power
(3, -1, 2, 0)

(3.0, -1.0, 2.0, 0.5)

1.0

4

• The boolean operators are spelled out as words and, not, or.
False

True

True

• Comparison operators >, <, >= (greater or equal), <= (less or equal), == equality, is identical.
(True, False)

(False, False)

(True, True)

True

True


Compound types: Strings, List and dictionaries

Strings

Strings are the variable type that is used for storing text messages.

str

11

Hello test


We can index a character in a string using []:

'H'


Heads up MATLAB users: Indexing start at 0!

We can extract a part of a string using the syntax [start:stop], which extracts characters between index start and stop:

'Hello'


If we omit either (or both) of start or stop from [start:stop], the default is the beginning and the end of the string, respectively:

'Hello'

'world'

'Hello world'


We can also define the step size using the syntax [start:end:step] (the default value for step is 1, as we saw above):

'Hello world'

'Hlowrd'


This technique is called slicing. Read more about the syntax here: http://docs.python.org/release/2.7.3/library/functions.html?highlight=slice#slice

Python has a very rich set of functions for text processing. See for example http://docs.python.org/2/library/string.html for more information.

String formatting examples

('str1', 'str2', 'str3')

('str1', 1.0, False, -1j)

str1str2str3

value = 1.000000

value1 = 3.14. value2 = 1

value1 = 3.1415, value2 = 1.5


List

Lists are very similar to strings, except that each element can be of any type.

The syntax for creating lists in Python is [...]:

<type 'list'>
[1, 2, 3, 4]


We can use the same slicing techniques to manipulate lists as we could use on strings:

[1, 2, 3, 4]
[2, 3]
[1, 3]


Heads up MATLAB users: Indexing starts at 0!

1


Elements in a list do not all have to be of the same type:

[1, 'a', 1.0, (1-1j)]


Python lists can be inhomogeneous and arbitrarily nested:

[1, [2, [3, [4, [5]]]]]


Lists play a very important role in Python, and are for example used in loops and other flow control structures (discussed below). There are number of convenient functions for generating lists of various types, for example the range function:

[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

'Hello world'

['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

[' ', 'H', 'd', 'e', 'l', 'l', 'l', 'o', 'o', 'r', 'w']


Adding, inserting, modifying, and removing elements from lists

['A', 'd', 'd']


We can modify lists by assigning new values to elements in the list. In technical jargon, lists are mutable.

['A', 'p', 'p']

['A', 'd', 'd']


Insert an element at an specific index using insert

['i', 'n', 's', 'e', 'r', 't', 'A', 'd', 'd']


Remove first element with specific value using ‘remove’

['i', 'n', 's', 'e', 'r', 't', 'd', 'd']


Remove an element at a specific location using del:

['i', 'n', 's', 'e', 'r', 't']


See help(list) for more details, or read the online documentation

Tuples

Tuples are like lists, except that they cannot be modified once created, that is they are immutable.

In Python, tuples are created using the syntax (..., ..., ...), or even ..., ...:

((10, 20), <type 'tuple'>)

((10, 20), <type 'tuple'>)


We can unpack a tuple by assigning it to a comma-separated list of variables:

('x =', 10)
('y =', 20)


If we try to assign a new value to an element in a tuple we get an error:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-83-ac1c641a5dca> in <module>()
----> 1 point[0] = 20

TypeError: 'tuple' object does not support item assignment


Dictionaries

Dictionaries are also like lists, except that each element is a key-value pair. The syntax for dictionaries is {key1 : value1, ...}:

<type 'dict'>
{'parameter1': 1.0, 'parameter3': 3.0, 'parameter2': 2.0}

parameter1 = 1.0
parameter2 = 2.0
parameter3 = 3.0

parameter1 = A
parameter2 = B
parameter3 = 3.0
parameter4 = D


Control Flow

Conditional statements: if, elif, else

The Python syntax for conditional execution of code use the keywords if, elif (else if), else:

statement1 and statement2 are False


For the first time, here we encounted a peculiar and unusual aspect of the Python programming language: Program blocks are defined by their indentation level.

Compare to the equivalent C code:

if (statement1)
{
printf("statement1 is True\n");
}
else if (statement2)
{
printf("statement2 is True\n");
}
else
{
printf("statement1 and statement2 are False\n");
}


In C blocks are defined by the enclosing curly brakets { and }. And the level of indentation (white space before the code statements) does not matter (completely optional).

But in Python, the extent of a code block is defined by the indentation level (usually a tab or say four white spaces). This means that we have to be careful to indent our code correctly, or else we will get syntax errors.

Examples:

both statement1 and statement2 are True

  File "<ipython-input-90-78979cdecf37>", line 4
print("both statement1 and statement2 are True")  # this line is not properly indented
^
IndentationError: expected an indented block

now outside the if block


Loops

In Python, loops can be programmed in a number of different ways. The most common is the for loop, which is used together with iterable objects, such as lists. The basic syntax is:

for loops:

1
2
3


The for loop iterates over the elements of the supplied list, and executes the containing block once for each element. Any kind of list can be used in the for loop. For example:

0
1
2
3


Note: range(4) does not include 4 !

-3
-2
-1
0
1
2

scientific
computing
with
python


To iterate over key-value pairs of a dictionary:

parameter4 = D
parameter1 = A
parameter3 = 3.0
parameter2 = B


Sometimes it is useful to have access to the indices of the values when iterating over a list. We can use the enumerate function for this:

(0, -3)
(1, -2)
(2, -1)
(3, 0)
(4, 1)
(5, 2)


List comprehensions: Creating lists using for loops:

A convenient and compact way to initialize lists:

[0, 1, 4, 9, 16]


while loops:

0
1
2
3
4
done


Note that the print("done") statement is not part of the while loop body because of the difference in indentation.

Functions

A function in Python is defined using the keyword def, followed by a function name, a signature within parentheses (), and a colon :. The following code, with one additional level of indentation, is the function body.

test


Optionally, but highly recommended, we can define a so called “docstring”, which is a description of the functions purpose and behaivor. The docstring should follow directly after the function definition, before the code in the function body.

Help on function func1 in module __main__:

func1(s)
Print a string 's' and tell how many characters it has

test has 4 characters


Functions that returns a value use the return keyword:

16


We can return multiple values from a function using tuples (see above):

(9, 27, 81)

27


Default argument and keyword arguments

In a definition of a function, we can give default values to the arguments the function takes:

If we don’t provide a value of the debug argument when calling the the function myfunc it defaults to the value provided in the function definition:

25

evaluating myfunc for x = 5 using exponent p = 2

25


If we explicitly list the name of the arguments in the function calls, they do not need to come in the same order as in the function definition. This is called keyword arguments, and is often very useful in functions that takes a lot of optional arguments.

evaluating myfunc for x = 7 using exponent p = 3

343


Unnamed functions (lambda function)

In Python we can also create unnamed functions, using the lambda keyword:

(4, 4)


This technique is useful for example when we want to pass a simple function as an argument to another function, like this:

[9, 4, 1, 0, 1, 4, 9]

[9, 4, 1, 0, 1, 4, 9]


Classes

Classes are the key features of object-oriented programming. A class is a structure for representing an object and the operations that can be performed on the object.

In Python a class can contain attributes (variables) and methods (functions).

A class is defined almost like a function, but using the class keyword, and the class definition usually contains a number of class method definitions (a function in a class).

• Each class method should have an argument self as it first argument. This object is a self-reference.

• Some class method names have special meaning, for example:

• __init__: The name of the method that is invoked when the object is first created.
• __str__ : A method that is invoked when a simple string representation of the class is needed, as for example when printed.
• There are many more, see http://docs.python.org/2/reference/datamodel.html#special-method-names

To create a new instance of a class:

Point at [0.000000, 0.000000]


To invoke a class method in the class instance p:

Point at [0.250000, 1.500000]
Point at [1.000000, 1.000000]


Note that calling class methods can modifiy the state of that particular class instance, but does not effect other class instances or any global variables.

That is one of the nice things about object-oriented design: code such as functions and related variables are grouped in separate and independent entities.

Modules

One of the most important concepts in good programming is to reuse code and avoid repetitions.

The idea is to write functions and classes with a well-defined purpose and scope, and reuse these instead of repeating similar code in different part of a program (modular programming). The result is usually that readability and maintainability of a program is greatly improved. What this means in practice is that our programs have fewer bugs, are easier to extend and debug/troubleshoot.

Python supports modular programming at different levels. Functions and classes are examples of tools for low-level modular programming. Python modules are a higher-level modular programming construct, where we can collect related variables, functions and classes in a module. A python module is defined in a python file (with file-ending .py), and it can be made accessible to other Python modules and programs using the import statement.

Consider the following example: the file mymodule.py contains simple example implementations of a variable, function and a class:

Writing mymodule.py


We can import the module mymodule into our Python program using import:

Use help(module) to get a summary of what the module provides:

Help on module mymodule:

NAME
mymodule

FILE
/is/ei/btabibian/Documents/scientific-python-lectures/mymodule.py

DESCRIPTION
Example of a python module. Contains a variable called my_variable,
a function called my_function, and a class called MyClass.

CLASSES
MyClass

class MyClass
|  Example class.
|
|  Methods defined here:
|
|  __init__(self)
|
|  get_variable(self)
|
|  set_variable(self, new_value)
|      Set self.variable to a new value

FUNCTIONS
my_function()
Example function

DATA
my_variable = 0

0

0

10


If we make changes to the code in mymodule.py, we need to reload it using reload:

<module 'mymodule' from 'mymodule.pyc'>


Exceptions

In Python errors are managed with a special language construct called “Exceptions”. When errors occur exceptions can be raised, which interrupts the normal program flow and fallback to somewhere else in the code where the closest try-except statement is defined.

To generate an exception we can use the raise statement, which takes an argument that must be an instance of the class BaseException or a class derived from it.

---------------------------------------------------------------------------

Exception                                 Traceback (most recent call last)

<ipython-input-129-8f47ba831d5a> in <module>()
----> 1 raise Exception("description of the error")

Exception: description of the error


A typical use of exceptions is to abort functions when some error condition occurs, for example:

def my_function(arguments):

if not verify(arguments):
raise Exception("Invalid arguments")

# rest of the code goes here


To gracefully catch errors that are generated by functions and class methods, or by the Python interpreter itself, use the try and except statements:

try:
# normal code goes here
except:
# code for error handling goes here
# this code is not executed unless the code
# above generated an error


For example:

test
Caught an exception


To get information about the error, we can access the Exception class instance that describes the exception by using for example:

except Exception as e:

test
Caught an exception:name 'test' is not defined


• http://www.python.org - The official web page of the Python programming language.
• http://www.python.org/dev/peps/pep-0008 - Style guide for Python programming. Highly recommended.
• http://www.greenteapress.com/thinkpython/ - A free book on Python programming.
• Python Essential Reference - A good reference book on Python programming.

Versions

Software Version
Python 2.7.8 64bit [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
IPython 3.0.0
OS Linux 3.13.0 45 generic x86_64 with debian jessie sid
Mon Apr 13 12:00:31 2015 CEST

Tags:

Categories:

Updated: