Python training UGA 2017

A training to acquire strong basis in Python to use it efficiently

Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)

First steps

Repository containing the source of this training:

Why Python ?

  • A very nice and well-thought programming language.

  • Generalist: good for several applications, used with success in many fields.

  • For academic purposes (teaching, research): great scientific ecosystem.

Python: a language, an interpreter

Python is a programming language.

It is first an interpreted language (like Bash, Matlab, and in contrast with Fortran, C or C++), so we need an interpreter, i.e. a binary parsing Python code and running computer instructions.

Open a terminal. There are usually different python commands: python, python2 and python3.

Remark Python 2 / Python 3

2 languages, 2 interpreters, 2 commands

Python distributions

(advice on how to install a good scientific Python environment)

  • In some systems (cluster), there are modules:

    module load python

Do it yourself: personal work

Download the repository of this training (you need one of the programs git or mercurial). On Unix machines, use the commands:

git clone
# or with Mercurial:
# hg clone
cd py-training-2017
cat README.rst
make help

Warning: cat and make are Unix commands...

Do it yourself: execute a script with the command python3

Run a script with python3 (pyfiles/ is just a text file):

cat pyfiles/
python3 pyfiles/

(cat is a Unix command that print the content of a text file.)

Do it yourself: work interactively with ipython

The command ipython3 launch the program ipython, which is used for interactive python.

  • In ipython, you can execute a first interactive instruction:
In [3]:
2 + 2
In [1]:
3 / 4
  • Run the script from ipython.
In [2]:
run ../pyfiles/
Hello world
My name is Pierre
In [3]:
# name is defined in ../pyfiles/
  • Help on an object can be displayed with the question mark (try it):
  • Try to type "name." and to press on tab... The tab key is used to tell you how what you typed can be completed.

  • Try to use the top and bottom arrows...

Python in an IDE (Spyder)

Launch the application Spyder, a Python IDE (Integrated development environment).

  • a quite good code editor with:
    • syntax coloring,
    • code analysis powered by pyflakes and pylint,
    • introspection capabilities such as code completion.
  • Ipython console
  • variable inspector
  • ...


Python in the browser (Jupyter)

The presentations of this python training are made with Jupyter (demonstration).

This is a very powerful tool to present results (see these examples).