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)

Characteristics of the Python language

Characteristics of the Python language

  • Open-source language, interpreters and ecosystem

  • Interpreted (but there are tools to compile Python code)

  • Automatic memory management

  • Dynamically strongly typed

Dynamically and strongly typed: objects and variables

The function type returns the type of an object:

In [5]:
type('hello')
Out[5]:
str
In [6]:
type(2)
Out[6]:
int
In [7]:
type(2.)
Out[7]:
float
In [8]:
type(2 + 2)
Out[8]:
int
In [9]:
type(2 + 2.)
Out[9]:
float
In [10]:
type(True)
Out[10]:
bool

Dynamically and strongly typed: objects and variables

Variables are just tags pointing towards objects. New variables can be used when needed. They are not associated with a type but only with an object (which has a type)...

In [11]:
myvar = 1
print(myvar, type(myvar))
1 <class 'int'>
In [12]:
myvar = 'hello'
print(myvar, type(myvar))
hello <class 'str'>

Spaces for objects and variables (names)

Objects and variables (names) are two very different concepts:

  • Objects live in one "object space". They have an address in the memory.
  • Names live in namespaces.

It is often interesting to represent the execution of a Python program in an "object space - namespaces" diagram.

The Zen of Python says "Namespaces are one honking great idea -- let's do more of those!". A namespace is created for every module (file) and for every function execution,

Characteristics of the Python language

  • Open-source language, interpreters and ecosystem

  • Interpreted (but there are tools to compile Python code)

  • Automatic memory management

  • Dynamically strongly typed

  • Gradual learning curve

  • A philosophy: the Zen of Python

  • Very clean and readable

  • Indentation defines the blocks

  • Style coding is important: pep8

PEP8 (Python Enhancement Proposal)

https://www.python.org/dev/peps/pep-0008/

  • Code layout
  • Imports
  • Whitespace in expressions and statements
  • Comments
  • Documentation strings
  • Naming conventions
  • Programming recommendations

PEP8: examples of bad and good style practices

In [1]:
# bad (spaces between operator)
number=0
# ok
number = 0
In [2]:
# bad (indentation with 2 spaces, has to be 4)
if number == 0:
  number = 1

# ok
if number == 0:
    number = 1 
In [3]:
# bad (space after ,)
mylist = [1,2,3]

# ok
mylist = [1, 2, 3]

Characteristics of the Python language

  • Open-source language, interpreters and ecosystem

  • Interpreted (but there are tools to compile Python code)

  • Automatic memory management

  • Dynamically strongly typed

  • Gradual learning curve

  • A philosophy: the Zen of Python

  • Very clean and readable

  • Indentation defines the blocks

  • Style coding is important: pep8

You have to use a good editor!!

Python:

  • (most of the time) interpreted (no compilation)
  • very dynamic
  • pep8 (style).
  • indentation defines the blocks

The editor can and has to help you!!

  • syntax coloring
  • automatic indentation
  • code analysis (code analysis powered by pyflakes and pylint)
  • introspection capabilities such as code completion

Do it yourself: PEP8 checker with Spyder editor

In Spyder, open the file ./pyfiles/wrong.py and see what happens.

Activate the "Real time code style analysis (PEP 8)" (Preferences -> Editor -> Code Introspection/Analysis).

Correct the file to remove the warnings.

Characteristics of the Python language

  • Open-source language, interpreters and ecosystem

  • Interpreted (but there are tools to compile Python code)

  • Automatic memory management

  • Dynamically strongly typed

  • Gradual learning curve

  • A philosophy: the Zen of Python

  • Very clean and readable

  • Indentation defines the blocks

  • Style coding is important: pep8

  • Only few keywords and built-in functions

Keywords

In [4]:
help("keywords")
Here is a list of the Python keywords.  Enter any keyword to get more help.

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

Characteristics of the Python language

  • Open-source language, interpreters and ecosystem

  • Interpreted (but there are tools to compile Python code)

  • Automatic memory management

  • Dynamically strongly typed

  • Gradual learning curve

  • A philosophy: the Zen of Python

  • Very clean and readable

  • Indentation defines the blocks

  • Style coding is important: pep8

  • Only few keywords and built-in functions

  • Errors should never pass silently

  • Multi-paradigm (sequential, object-oriented, functional)

  • "Batteries Included": the standard library