Basics of Python language

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.

Fundamental types / lists / touples

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


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:


Are like lists, except non-modifiable.

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


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 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:


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).


For additional modularity, modules are provided in Python.

The modularity in Python is:

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).