## Variables and type¶

### Symbol names¶

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.

## Formating strings and print statement¶

Python2 type of printing, still very useful

More Python3 oriented printing

We can also skip default values, like position argument or types

## Lists¶

The syntax for creating list is [..,..,]

The index starts with 0, like in C (not like in Fortran). Negative indices allowed.

List can contain different types, and can be nested

Slicing works just as with strings or arrays. Notation [start:end:step], defaults can be omitted

Modifying a list:

• append
• insert
• remove
• del

## Tuples¶

Are like lists, except non-modifiable.

Most important use in returning from a function, and use in print statement.

## Dictionaries¶

Dictionaries are like lists, but their keys are not integers, rather key can be any scalar type or tuple of scalar types

Dictionary is constructed by {...}

## Control flow¶

• if, else, elif

• loops using for & while

• List comprehensions: inline for statement

[ < do something with i > for i in data]

## Functions¶

• A functions starts with keyword def
• Very useful is the docstring, i.e., a string at the begining, which explains what the function does
• Multiple things (classes or values) can be returned by tuples
• function can have default and keyword arguments

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.

## Unnamed functions (lambda function)¶

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

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

## Classes¶

A class can contain attributes (variables) and methods (functions).

A class is defined by class keyword, and the class can contains a number of class method definitions (a function in a class).

• Each class method should have an argument self as its first argument. This object is a self-reference.
• Some class method names have special meaning, for example:
• __init__ : Constructor, i.e., 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.
• __repr__ : Representation of the class when printed
• __call__ : Functor, i.e, called when the instance is “called” as a function
• There are many more, see http://docs.python.org/2/reference/datamodel.html#special-method-names

## Modules¶

For additional modularity, modules are provided in Python.

The modularity in Python is:

• variables, functions
• classes, which combine variables and functions
• modules, which combine classes, variables and functions into a unit, usually a file

Module is a python file (*.py) or a module created by compiler (*.so)

Outside jupyter (in regular python) we just create a file with the code, and call it a module.

Within jupyter, we need to write code into a file, and than load the module (file).