Commit 00b8c248 authored by Franck Thollard's avatar Franck Thollard
Browse files

correction of the index of the value of the error

parents c715603b 7c9c74eb
syntax: glob
*.pyd*
*.pyc
*~
......@@ -8,9 +6,14 @@ syntax: glob
\#*\#
ipynb/*.slides.html
.ipynb_checkpoints
*/.ipynb_checkpoints/*
**/Untitled.ipynb
**/*.egg-info/
ipynb/index.html
\ No newline at end of file
ipynb/index.html
.vscode
TP/TP2_package/mypackuga/build
TP/TP2_package/mypackuga/dist
\ No newline at end of file
......@@ -37,7 +37,7 @@ ipynb/index.html: ipynb/index.rst
cd $(IPYNBDIR) && rst2html5 index.rst > index.html
%.slides.html: %.ipynb ipynb/slides_reveal_wide.tpl
jupyter-nbconvert $< --to slides --template ipynb/slides_reveal_wide.tpl
jupyter-nbconvert $< --reveal-prefix='./reveal.js' --to slides --template ipynb/slides_reveal_wide.tpl
presentations: $(IPYNBPRES) ipynb/index.html
......
......@@ -9,12 +9,10 @@ To display and/or modify the presentations, use the Makefile. See::
make help
Repositories
------------
Repository
----------
The old repository is https://sourcesup.renater.fr/projects/py-training-uga/
We now use a repository hosted in the Gitlab of UGA:
The repository hosted in the Gitlab of UGA:
https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga
- Clone the repository with Git with https::
......@@ -40,5 +38,4 @@ Authors
-------
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck
Thollard (ISTERRE), Christophe Picard (LJK)
Thollard (ISTERRE), Christophe Picard (LJK), Loïc Huder (ISTerre)
......@@ -6,7 +6,7 @@ form key=val where key is p1, p2 or p3, and val is a float.
import re
import logging
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
def compute_stats(file_name):
"""
......@@ -60,7 +60,7 @@ def check_format_re(file_name, default_values):
for i,v in enumerate(vals):
if v is None:
logging.warning(f'file {file_name} line {line_num}: missing: p{i+1}, setting default value')
vals[i] = default_values[i-1]
vals[i] = default_values[i]
def check_format(file_name, cols_names_set):
......
......@@ -12,7 +12,7 @@
"\n",
"**A training to acquire strong basis in Python to use it efficiently**\n",
"\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK) Loïc Huder (ISTerre)\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)\n",
"\n",
"# First steps\n",
"\n",
......@@ -274,9 +274,7 @@
"\n",
"Remarks: \n",
"\n",
"- it is very important to use a good editor to code in Python (for example spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d)).\n",
"\n",
"- a good modern open-source editor: [Visual Studio Code] (https://code.visualstudio.com/docs/languages/python)."
"- it is very important to use a good editor to code in Python (for example Spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d), or [Visual Studio Code](https://code.visualstudio.com/docs/languages/python))."
]
},
{
......
%% Cell type:markdown id: tags:
# 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)
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:
`https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga/py-training-2017`
%% Cell type:markdown id: tags:
## Why Python ?
- A very nice and well-thought programming language.
- **Generalist**: good for several applications, [used with success in many fields](http://brochure.getpython.info/).
- For academic purposes (teaching, research): **great scientific ecosystem**.
%% Cell type:markdown id: tags:
## 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`.
%% Cell type:markdown id: tags:
### Remark Python 2 / Python 3
2 languages, 2 interpreters, 2 commands
%% Cell type:markdown id: tags:
## Python distributions
- On Linux, there are the system python binaries.
- On Windows, Mac (and Linux), you can install and use
* https://www.python.org/downloads/
* [Anaconda or Miniconda](https://www.continuum.io/downloads)
([advice on how to install a good scientific Python environment](http://fluiddyn.readthedocs.io/en/latest/get_good_Python_env.html))
- In some systems (cluster), there are modules:
```bash
module load python
```
%% Cell type:markdown id: tags:
### 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:
```bash
git clone https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga/py-training-2017.git
# or with Mercurial:
# hg clone https://gricad-gitlab.univ-grenoble-alpes.fr/python-uga/py-training-2017.git
cd py-training-2017
cat README.rst
make help
```
Warning: `cat` and `make` are Unix commands...
%% Cell type:markdown id: tags:
### Do it yourself: execute a script with the command `python3`
Run a script with `python3` (pyfiles/helloworld.py is *just* a text file):
```bash
cat pyfiles/helloworld.py
python3 pyfiles/helloworld.py
```
(`cat` is a Unix command that print the content of a text file.)
%% Cell type:markdown id: tags:
### Do it yourself: work interactively with [ipython](https://ipython.org/)
The command `ipython3` launch the program [ipython](https://ipython.org/), which is used for interactive python.
- In ipython, you can execute a first interactive instruction:
%% Cell type:code id: tags:
``` python
2 + 2
```
%%%% Output: execute_result
4
%% Cell type:code id: tags:
``` python
3 / 4
```
%%%% Output: execute_result
0.75
%% Cell type:markdown id: tags:
- Run the script from ipython.
%% Cell type:code id: tags:
``` python
run ../pyfiles/helloworld.py
```
%%%% Output: stream
Hello world
My name is Pierre
%% Cell type:code id: tags:
``` python
# name is defined in ../pyfiles/helloworld.py
print(name)
```
%%%% Output: stream
Pierre
%% Cell type:markdown id: tags:
- Help on an object can be displayed with the question mark (try it):
```ipython
name?
```
- 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...
%% Cell type:markdown id: tags:
### Python in an IDE ([Spyder](https://pythonhosted.org/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
- ...
Remarks:
- it is very important to use a good editor to code in Python (for example spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d)).
- a good modern open-source editor: [Visual Studio Code] (https://code.visualstudio.com/docs/languages/python).
- it is very important to use a good editor to code in Python (for example Spyder, emacs or vi with [a good setup](https://bitbucket.org/fluiddyn/fluid_emacs.d), or [Visual Studio Code](https://code.visualstudio.com/docs/languages/python)).
%% Cell type:markdown id: tags:
### Python in the browser ([Jupyter](https://jupyter.readthedocs.io))
The presentations of this python training are made with Jupyter (demonstration).
This is a very powerful tool to present results (see these [examples](http://nbviewer.jupyter.org/)).
......
......@@ -12,8 +12,7 @@
"\n",
"**A training to acquire strong basis in Python to use it efficiently**\n",
"\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK),\n",
" Loïc Huder (ISTerre)\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)\n",
" \n",
"\n",
"# Characteristics of the Python language"
......
%% Cell type:markdown id: tags:
# 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)
Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)
# Characteristics of the Python language
%% Cell type:markdown id: tags:
## 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 (see next slides)
- Gradual learning curve
- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)
- Very clean and readable
- Indentation defines the blocks
- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/) (see next slides)
- Only few [keywords](https://hg.python.org/cpython/file/3.6/Lib/keyword.py) and [built-in functions](https://docs.python.org/3/library/functions.html) (see next slides)
- Errors should never pass silently
- Multi-paradigm (sequential, object-oriented, functional)
- "Batteries Included": [the standard library](https://docs.python.org/3/tutorial/stdlib.html)
%% Cell type:markdown id: tags:
## PEP8 (Python Extension Proposal)
https://www.python.org/dev/peps/pep-0008/
- Code layout
- Imports
- Whitespace in expressions and statements
- Comments
- Documentation strings
- Naming conventions
- Programming recommendations
%% Cell type:markdown id: tags:
## PEP8: examples of bad and good style practices
%% Cell type:code id: tags:
``` python
# bad (spaces between operator)
number=0
# ok
number = 0
```
%% Cell type:code id: tags:
``` python
# bad (indentation with 2 spaces, has to be 4)
if number == 0:
number = 1
# ok
if number == 0:
number = 1
```
%% Cell type:code id: tags:
``` python
# bad (space after ,)
mylist = [1,2,3]
# ok
mylist = [1, 2, 3]
```
%% Cell type:markdown id: tags:
## Keywords
%% Cell type:code id: tags:
``` python
help("keywords")
```
%%%% Output: stream
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
%% Cell type:markdown id: tags:
plus 68 [built-in functions](https://docs.python.org/3/library/functions.html)... plus few [built-in constants](https://docs.python.org/3/library/constants.html)... plus [built-in exceptions](https://docs.python.org/3/library/exceptions.html)!
%% Cell type:markdown id: tags:
## Dynamically and strongly typed: objects and variables
The function `type` returns the type of an **object**:
%% Cell type:code id: tags:
``` python
type('hello')
```
%%%% Output: execute_result
str
%% Cell type:code id: tags:
``` python
type(2)
```
%%%% Output: execute_result
int
%% Cell type:code id: tags:
``` python
type(2.)
```
%%%% Output: execute_result
float
%% Cell type:code id: tags:
``` python
type(2 + 2)
```
%%%% Output: execute_result
int
%% Cell type:code id: tags:
``` python
type(2 + 2.)
```
%%%% Output: execute_result
float
%% Cell type:code id: tags:
``` python
type(True)
```
%%%% Output: execute_result
bool
%% Cell type:markdown id: tags:
## 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)...
%% Cell type:code id: tags:
``` python
myvar = 1
print(myvar, type(myvar))
```
%%%% Output: stream
1 <class 'int'>
%% Cell type:code id: tags:
``` python
myvar = 'hello'
print(myvar, type(myvar))
```
%%%% Output: stream
hello <class 'str'>
%% Cell type:markdown id: tags:
## 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,
%% Cell type:markdown id: tags:
## 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
%% Cell type:markdown id: tags:
## Do it yourself: PEP8 checker with Spyder editor
In Spyder, open the file `./pyfiles/wrong.py` and see what append.
Activate the "Real time code style analysis (PEP 8)" (Preferences -> Editor -> Code Introspection/Analysis).
Correct the file to remove the warnings.
......
......@@ -902,7 +902,7 @@
" | a | b | c | d | e | f | \n",
" +---+---+---+---+---+---+ \n",
"Slice from front: 0 1 2 3 4 5 6 \n",
"Slice from rear: -6 -5 -4 -3 -2 -1 0\n",
"Slice from rear: -6 -5 -4 -3 -2 -1 None\n",
"```"
]
},
......@@ -2486,7 +2486,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
"version": "3.6.6"
}
},
"nbformat": 4,
......
%% Cell type:markdown id: tags:
# 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)
# More standard types and basic statements
%% Cell type:markdown id: tags:
## More on standard types
- Simple types (`int`, `float`, `bool`, `complex`)
- Standard type `str`
- Standard type `list`
- Standard type `tuple`
%% Cell type:markdown id: tags:
### `int` (integers)
%% Cell type:code id: tags:
``` python
a = 4
c = -10
# binary notation (base 2)
b = 0b010
# octal notation (base 8)
o = 0o011
# hexadecimal (base 16)
h = 0x1cf0
a = int('1') # base 10
a = int('111', 2) # base 2
a = int('70', 8) # base 8
a = int('16', 16) # base 16
```
%% Cell type:markdown id: tags:
Remark: `int` in Python 3 are impressive! No limit! See https://docs.python.org/3.1/whatsnew/3.0.html#integers
%% Cell type:markdown id: tags:
## Arithmetic operations
%% Cell type:code id: tags:
``` python
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3) # float division
print(10 // 3) # integer division
print(10 % 3)
```
%%%% Output: stream
13
7
30
3.3333333333333335
3
1
%% Cell type:markdown id: tags:
### `bool` (booleans)
%% Cell type:code id: tags:
``` python
b = bool('1')
b = False
b = True
```
%% Cell type:markdown id: tags:
##### Comparison operations (bool)
- `==` equal
- `!=` différent
- `<` inferior
- `<=` inferior or equal
- `>` superior
- `>=` superior or equal
%% Cell type:markdown id: tags:
##### Keyword `is`: check identity
%% Cell type:code id: tags:
``` python
a = None
print(a is None)
print(a is not None)
```
%%%% Output: stream
True
False
%% Cell type:markdown id: tags:
##### Keywords `and` and `or`
%% Cell type:code id: tags:
``` python
True and True
```
%%%% Output: execute_result
True
%% Cell type:code id: tags:
``` python
True and False
```
%%%% Output: execute_result
False
%% Cell type:code id: tags:
``` python
False and False
```
%%%% Output: execute_result
False
%% Cell type:code id: tags:
``` python
True or True
```
%%%% Output: execute_result
True
%% Cell type:code id: tags:
``` python
True or False
```
%%%% Output: execute_result
True
%% Cell type:code id: tags:
``` python
False or False
```
%%%% Output: execute_result
False
%% Cell type:markdown id: tags:
### `float` (real, double precision) and `complex`
%% Cell type:code id: tags:
``` python
# float
a = float('1')
a = 1.234
a = 1e2
a = -1e-2
a = .2
```
%% Cell type:code id: tags:
``` python
# complex (2 floats)
c = complex('1')
c = 1 + 2j
print(c, c.real, c.imag)
```
%%%% Output: stream
(1+2j) 1.0 2.0
%% Cell type:markdown id: tags:
Remark: notation `var_name.attr_name` to access to an attribute of an object.
%% Cell type:markdown id: tags:
## Warning about floating-point arithmetic and numerical errors!
%% Cell type:code id: tags:
``` python
b = 1e16
c = 1.2 + b
d = c - b
print(d)
```
%%%% Output: stream
2.0
%% Cell type:markdown id: tags:
Very general issue (not Python):
see https://en.wikipedia.org/wiki/Floating-point_arithmetic
%% Cell type:markdown id: tags:
### Standard type `str`
%% Cell type:code id: tags:
``` python
s = 'hello'
s = "hello"
s = ('How is it possible to write a very very '
'very long string with lines limited to 79 characters?')
s = """Strings on
more thand
one line.
"""
print(s)
```
%%%% Output: stream
Strings on
more thand
one line.
%% Cell type:markdown id: tags:
Warning: big difference between Python 2 and Python 3. In Python 3, `str` are unicode and there is another type `bytes`.
%% Cell type:markdown id: tags:
#### Methods of the type `str`
Objects of built-in types have methods associated with their type (object oriented programming). The built-in function `dir` returns a list of name of the attributes. For a string, these attributes are python system attributes (with double-underscores) and several public methods:
%% Cell type:code id: tags:
``` python
s = 'abcdef'
print(dir(s))
```
%%%% Output: stream
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
%% Cell type:markdown id: tags:
To access an attribute of an object (here, the method `str.startswith`), we use the dot:
%% Cell type:code id: tags:
``` python
s.startswith('a')
```
%%%% Output: execute_result
True
%% Cell type:markdown id: tags:
### standard type `str`
#### function `str.format`
```text
Docstring:
S.format(*args, **kwargs) -> str
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').
```
%% Cell type:code id: tags:
``` python
a = 1.23456789
'a = {}'.format(a)
```
%%%% Output: execute_result
'a = 1.23456789'
%% Cell type:code id: tags:
``` python
'a = {:.4f}'.format(a)
```
%%%% Output: execute_result
'a = 1.2346'
%% Cell type:code id: tags:
``` python
'a = {:8.4f}'.format(a)
```
%%%% Output: execute_result
'a = 1.2346'
%% Cell type:code id: tags:
``` python
'a = {:.4e} (scientific notation)'.format(a)
```
%%%% Output: execute_result
'a = 1.2346e+00 (scientific notation)'
%% Cell type:code id: tags:
``` python
print('{}\t{}\t{}'.format(1, 2, 3))
```
%%%% Output: stream
1 2 3
%% Cell type:markdown id: tags:
### standard type `str`
#### New in Python 3.6: format strings
%% Cell type:code id: tags:
``` python
a = 1.23456789
f'a = {a}'
```
%%%% Output: execute_result
'a = 1.23456789'
%% Cell type:code id: tags:
``` python
f'a = {a:.4f}'
```
%%%% Output: execute_result
'a = 1.2346'
%% Cell type:code id: tags:
``` python
f'a = {a:8.4f}'
```
%%%% Output: execute_result
'a = 1.2346'
%% Cell type:code id: tags:
``` python
f'a = {a:.4e} (scientific notation)'
```
%%%% Output: execute_result
'a = 1.2346e+00 (scientific notation)'
%% Cell type:code id: tags:
``` python
print(f'{1}\t{1+1}\t{2+1}')
```
%%%% Output: stream
1 2 3
%% Cell type:markdown id: tags:
### standard type `str`
Strings are immutable **["sequences"](https://docs.python.org/3/library/stdtypes.html)**.
- lookup
%% Cell type:code id: tags:
``` python
s = 'abcdef'
print('a' in s)
print('hello' not in s)
```
%%%% Output: stream
True
True
%% Cell type:markdown id: tags:
- We can get an element of a string (**index starts from 0**):
%% Cell type:code id: tags:
``` python
print(s[0])
```
%%%% Output: stream
a
%% Cell type:markdown id: tags:
- since strings are immutable, they can not be modified inplace. If we try, we get an error:
```python
s[0] = 'b'
```
```text
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-55620f378bce> in <module>()
----> 1 s[0] = 'b'
TypeError: 'str' object does not support item assignment
```
- since strings are sequences, they can be "sliced" (we will soon study in details this powerful notation):
%% Cell type:code id: tags:
``` python
s[1:3]
```
%%%% Output: execute_result
'bc'
%% Cell type:markdown id: tags:
- it is very simple to manipulate strings in many ways:
%% Cell type:code id: tags:
``` python
print((s.capitalize() + ' ' + s.upper() + '\n') * 4 )
```
%%%% Output: stream
Abcdef ABCDEF
Abcdef ABCDEF
Abcdef ABCDEF
Abcdef ABCDEF
%% Cell type:markdown id: tags:
### Slicing
Very general, can be used on all sequences as `str`, `list`, etc... Not simple for beginners but very powerfull (see [here](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) and [here](http://avilpage.com/2015/03/a-slice-of-python-intelligence-behind.html)).
Python indexes and slices for a six-element str. Indexes enumerate the elements, slices enumerate the spaces between the elements.
```text
Index from rear: -6 -5 -4 -3 -2 -1
Index from front: 0 1 2 3 4 5
+---+---+---+---+---+---+
| a | b | c | d | e | f |
+---+---+---+---+---+---+
Slice from front: 0 1 2 3 4 5 6
Slice from rear: -6 -5 -4 -3 -2 -1 0
Slice from rear: -6 -5 -4 -3 -2 -1 None
```
%% Cell type:code id: tags:
``` python
s = 'abcdef'
# s[start:stop:step]
s[2:6:2]
```
%%%% Output: execute_result
'ce'
%% Cell type:code id: tags:
``` python
# s[start:stop]
s[2:6]
```
%%%% Output: execute_result
'cdef'
%% Cell type:code id: tags:
``` python
# s[start:]
s[1:]
```
%%%% Output: execute_result
'bcdef'
%% Cell type:code id: tags:
``` python
# s[:stop]
s[:2]
```
%%%% Output: execute_result
'ab'
%% Cell type:code id: tags:
``` python
# step = -1: goes through the string in reverse order
s[::-1]
```
%%%% Output: execute_result
'fedcba'
%% Cell type:markdown id: tags:
### Do it yourself
Suppose we have a string representing a header line of the form:
%% Cell type:code id: tags:
``` python
s = ' wind;temperature;;pressure '
```
%% Cell type:markdown id: tags:
- Remove leading and ending blanks (see `str.replace` and `str.strip`)
- Extract the first field (e.g. using `find` method and slicing)
- Extract the last field (e.g. using `rfind` method and slicing)
- Check for empty field (e.g. `find` ";;" pattern)
- Remove empty field (e.g. using replace method)
- BONUS : Extract the second field (tip :`find` can take an argument that tells where to start the search)
%% Cell type:markdown id: tags:
#### A possible solution:
%% Cell type:code id: tags:
``` python
s = ' wind;temperature;;pressure '
# remove leading blanks
s = s.strip()
print("--{}--".format(s))
# extract the first field
idx = s.find(";")
s0 = s[0:idx]
print(s0)
# extract the second field
idx1 = s.find(";", idx+1) # start the search after the first ";"
s1 = s[idx+1:idx1]
print(s1)
# extract the last field
idx2 = s.rfind(";")
s2 = s[idx2+1:]
print(s2)
idx_first_empty_field = s.find(";;")
print(idx_first_empty_field)
# remove empty field
s_no_empty = s.replace(";;", ";")
print(s_no_empty)
```
%%%% Output: stream
--wind;temperature;;pressure--
wind
temperature
pressure
16
wind;temperature;pressure
%% Cell type:markdown id: tags:
### standard type `list`
A list is a **mutable sequence of (possibly inhomogeneous) elements**.
%% Cell type:code id: tags:
``` python
type([0, 'a'])
```
%%%% Output: execute_result
list
%% Cell type:code id: tags:
``` python
# create an empty list
l = []
# fill the list (with the function append)
l.append('2')
# fill the list (with the function extend)
l.extend([6, 3.])
print(l)
```
%%%% Output: stream
['2', 6, 3.0]
%% Cell type:code id: tags:
``` python
# concatenate lists with the operator +
print(l + ['hello', 3])
```
%%%% Output: stream
['2', 6, 3.0, 'hello', 3]
%% Cell type:code id: tags:
``` python
# get values
print(l[0], l[2], l[-2])
# slicing
print(l[0:2])
```
%%%% Output: stream
2 3.0 6
['2', 6]
%% Cell type:markdown id: tags:
### standard type `tuple`
A tuple is a **immutable sequence of (possibly inhomogeneous) elements**.
Remark: when you need a sequence that won't be modified, tuple is usually more efficient than list.
%% Cell type:code id: tags:
``` python
t = 0, 'a', 1.2
t1 = (5, 'hello')
t2 = tuple([1.1, 2])
type(t)
```
%%%% Output: execute_result
tuple
%% Cell type:code id: tags:
``` python
t[1] # indexing
```
%%%% Output: execute_result
'a'
%% Cell type:code id: tags:
``` python
t[1:] # slicing
```
%%%% Output: execute_result
('a', 1.2)
%% Cell type:code id: tags:
``` python
a, b = t1 # tuple assigment
print(b)
```
%%%% Output: stream
hello
%% Cell type:markdown id: tags:
## Mutable and immutable objects
The objects of type `str`, `int`, `float`, `bool` are immutable. They can not be modified. Of course, a name that points towards an integer can point towards a different integer.
%% Cell type:code id: tags:
``` python
i = 1
i = i + 2 # (or i += 2)
print(i)
i = 10
print(i)
```
%%%% Output: stream
3
10
%% Cell type:markdown id: tags:
Here, the objects `1` and `3` have not been modified.
%% Cell type:markdown id: tags:
## Mutable and immutable objects
An object of type `list` is mutable:
%% Cell type:code id: tags:
``` python
l = [0, 5]
print(l)
l.append('hello')
print(l)
```
%%%% Output: stream
[0, 5]
[0, 5, 'hello']
%% Cell type:markdown id: tags:
Here, the object list tagged by the name `l` has been modified inplace.
%% Cell type:markdown id: tags:
## References and `del` keyword
`del` removes a reference. If an object in not binded to any names, Python can delete it from its internal memory.
%% Cell type:code id: tags:
``` python
l = ['a', 'b']
del l[1]
print(l)
```
%%%% Output: stream
['a']
%% Cell type:markdown id: tags:
### More on slicing
Very general, can be used on all sequences as `str`, `list`, etc... Not simple for beginners but very powerfull (see [here](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) and [here](http://avilpage.com/2015/03/a-slice-of-python-intelligence-behind.html)).
Python indexes and slices for a six-element str. Indexes enumerate the elements, slices enumerate the spaces between the elements.
```text
Index from rear: -6 -5 -4 -3 -2 -1
Index from front: 0 1 2 3 4 5
+---+---+---+---+---+---+
| a | b | c | d | e | f |
+---+---+---+---+---+---+
Slice from front: 0 1 2 3 4 5 6
Slice from rear: -6 -5 -4 -3 -2 -1 0
```
%% Cell type:code id: tags:
``` python
s = 'abcdef'
# s[start:stop:step]
s[2:6:2]
```
%%%% Output: execute_result
'ce'
%% Cell type:markdown id: tags:
### More on slicing
#### Assigment to mutable object
%% Cell type:code id: tags:
``` python
l = [0, 1, 2, 3, 4, 5]
l1 = l # assigment to a new name l1 (no copy of the object).
# the names l and l1 points towards the same object.
l1.append('a')
print(l1)
print(l)
```
%%%% Output: stream
[0, 1, 2, 3, 4, 5, 'a']
[0, 1, 2, 3, 4, 5, 'a']
%% Cell type:markdown id: tags:
#### Shallow copy
%% Cell type:code id: tags:
``` python
l = [0, 1, 2, 3, 4, 5]
l1 = l[:] # shallow copy of l
l1.append('a')
print(l1)
print(l)
```
%%%% Output: stream
[0, 1, 2, 3, 4, 5, 'a']
[0, 1, 2, 3, 4, 5]
%% Cell type:markdown id: tags:
### More on slicing
Other examples of slices for a six-element list. Indexes enumerate the elements, slices enumerate the spaces between the elements.
%% Cell type:code id: tags:
``` python
a = [0, 1, 2, 3, 4, 5]
all([
len(a) == 6,
a[1:] == [1, 2, 3, 4, 5],
a[:5] == [0, 1, 2, 3, 4],
a[0] == 0,
a[:-2] == [0, 1, 2, 3],
a[5] == 5,
a[1:2] == [1],
a[-1] == 5,
a[1:-1] == [1, 2, 3, 4],
a[-2] == 4,
])
```
%%%% Output: execute_result
True
%% Cell type:markdown id: tags:
### Do it yourself
Suppose we have the string containing header line.
%% Cell type:code id: tags:
``` python
s = 'wind;temperature;pressure'
```
%% Cell type:markdown id: tags:
- Extract the list of items (i.e. "wind", "temperature", "pressure"; see `str.split`).
- Add "Snow level" to the list of items (e.g. using append)
- Build a new header such that items are capitalized (e.g. using the methods `str.join` and `str.capitalize` and iterating on the list)
%% Cell type:markdown id: tags:
#### A possible solution:
%% Cell type:code id: tags:
``` python
s = 'wind;temperature;pressure'
list_items = s.split(";")
print(list_items)
list_items.append("snow level".capitalize())
list_items[0] = list_items[0].capitalize()
list_items[1] = list_items[1].capitalize()
list_items[2] = list_items[2].capitalize()
print(list_items)
";".join(list_items)
```
%%%% Output: stream
['wind', 'temperature', 'pressure']
['Wind', 'Temperature', 'Pressure', 'Snow level']
%%%% Output: execute_result
'Wind;Temperature;Pressure;Snow level'
%% Cell type:markdown id: tags:
### The function `range`
The function returns a range object:
%% Cell type:code id: tags:
``` python
# start, stop, step
range(1, 8, 2)
```
%%%% Output: execute_result
range(1, 8, 2)
%% Cell type:markdown id: tags:
We can make a list with the range object:
%% Cell type:code id: tags:
``` python
# start, stop, step
list(range(1, 8, 2))
```
%%%% Output: execute_result
[1, 3, 5, 7]
%% Cell type:code id: tags:
``` python
# start, stop (step=1)
list(range(2, 8))
```
%%%% Output: execute_result
[2, 3, 4, 5, 6, 7]
%% Cell type:code id: tags:
``` python
# stop argument (start=0, step=1)
list(range(8))
```
%%%% Output: execute_result
[0, 1, 2, 3, 4, 5, 6, 7]
%% Cell type:markdown id: tags:
### Do it yourself
Build a list of odd numbers in decreasing order.
%% Cell type:markdown id: tags:
## Conditions: `if`, `elif`, `else`
```python
if expression:
statement(s)
else:
statement(s)
```
The statement contains the block of code that executes if the conditional expression in the if statement resolves to 1 or a TRUE value.
%% Cell type:code id: tags:
``` python
a = 0
if a == 0:
print('a is equal to 0.')
```
%%%% Output: stream
a is equal to 0.
%% Cell type:code id: tags:
``` python
a = 1
if a < 0:
print('a is negative.')
elif a == 0:
print('a is equal to 0.')
elif a > 0:
print('a is positive.')
else:
print("I don't know.")
```
%%%% Output: stream
a is positive.
%% Cell type:markdown id: tags:
## Loops
%% Cell type:markdown id: tags:
### Loops with the keyword `while`
%% Cell type:code id: tags:
``` python
i = 0
while i < 4:
i += 1
print('i =', i)
```
%%%% Output: stream
i = 4
%% Cell type:code id: tags:
``` python
i = 0
while i < 4:
i += 1
print('i =', i)
```
%%%% Output: stream
i = 1
i = 2
i = 3
i = 4
%% Cell type:markdown id: tags:
### Do it yourself
- Edit a script with the spyder editor that calculates the average of a set of numbers. For example `numbers = [67, 12, 2, 9, 23, 5]`
* using the functions `sum` and `len`
* manually, using the keyword `while`
- Run the script
* in spyder,
* in a ipython session opened from another terminal,
* with the command `python`.
%% Cell type:markdown id: tags:
#### A possible solution:
%% Cell type:code id: tags:
``` python
numbers = [67, 12, 2, 9, 23, 5]
local_sum = 0
i = 0
while i < len(numbers):
local_sum = local_sum + numbers[i]
i = i+1
avg = local_sum / len(numbers)
print(avg)
```
%%%% Output: stream
19.666666666666668
%% Cell type:markdown id: tags:
### Loops with the keyword `for`
%% Cell type:code id: tags:
``` python
values = range(5)
for value in values:
print(value, end=', ')
```
%%%% Output: stream
0, 1, 2, 3, 4,
%% Cell type:code id: tags:
``` python
# the built-in function enumerate is very useful
for index, value in enumerate(['a', 'b', 'c']):
print('({}, {})'.format(index, value))
```
%%%% Output: stream
(0, a)
(1, b)
(2, c)
%% Cell type:markdown id: tags:
### Loops: keywords `continue` and `break`
- `continue`: passes the block in the loop and continues the loop.
%% Cell type:code id: tags:
``` python
for x in range(1, 8):
if x == 5:
continue
print(x, end=', ')
```
%%%% Output: stream
1, 2, 3, 4, 6, 7,
%% Cell type:markdown id: tags:
- `break`: stop the loop.
%% Cell type:code id: tags:
``` python
for x in range(1, 11):
if x == 5:
break
print(x, end=', ')
```
%%%% Output: stream
1, 2, 3, 4,
%% Cell type:markdown id: tags:
### Do it yourself
- Simplify your script by using a `for` loops.
- In ipython, try to understand how the function `enumerate` works. Use it in your script.
%% Cell type:markdown id: tags:
#### A possible solution:
%% Cell type:code id: tags:
``` python
l = [67, 12, 2, 9, 23, 5]
local_sum = 0
for e in l:
local_sum += e
avg = local_sum / len(l)
print(avg)
```
%%%% Output: stream
19.666666666666668
%% Cell type:markdown id: tags:
### Do it yourself
We build a list:
%% Cell type:code id: tags:
``` python
from random import shuffle, randint
n = 20
i = randint(0, n-1)
print('integer remove from the list:', i)
l = list(range(n))
l.remove(i)
shuffle(l)
print('shuffled list: ', l)
```
%%%% Output: stream
integer remove from the list: 3
shuffled list: [13, 16, 6, 10, 17, 19, 4, 2, 5, 9, 0, 7, 11, 12, 14, 8, 1, 15, 18]
%% Cell type:markdown id: tags:
One element has been removed:
- Find this element (given that you can change the ordering of `l`).
- Find this element (given that you cannot change the ordering of `l`).
%% Cell type:markdown id: tags:
#### A possible solution:
%% Cell type:code id: tags:
``` python
# we can change ordering, let's sort
print(l)
l_sorted = sorted(l)
print(l_sorted)
found = None
for idx, elem in enumerate(l_sorted):
if elem != idx:
found = idx
break
if found is None:
found = len(l)
print("missing ", idx)
```
%%%% Output: stream
[13, 16, 6, 10, 17, 19, 4, 2, 5, 9, 0, 7, 11, 12, 14, 8, 1, 15, 18]
[0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
missing 3
%% Cell type:code id: tags:
``` python
# we cannot sort -> higher complexity
for elem in range(len(l)+1):
if elem not in l:
break
print("missing ", elem)
```
%%%% Output: stream
missing 3
%% Cell type:code id: tags:
``` python
# another solution
actual_sum = sum(l)
len_l = len(l)
original_sum = (len_l + 1) * (len_l) // 2
print("missing ", original_sum - actual_sum)
```
%%%% Output: stream
missing 3
%% Cell type:markdown id: tags:
## Exceptions and `try`, `except` syntax
%% Cell type:markdown id: tags:
Exceptions and errors are common in all codes. There is a good system to handle them in Python. Let's first see what gives such buggy code
```python
l = ['a']
i = 1
print(l[i])
```
When these lines are executed, Python stops its execution and print a traceback:
```text
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-30-8df9cec1a0ec> in <module>()
1 l = ['a']
2 i = 1
----> 3 print(l[i])
IndexError: list index out of range
```
%% Cell type:markdown id: tags:
## Exceptions and `try`, `except` syntax
Handling exception:
%% Cell type:code id: tags:
``` python
l = ['a']
i = 1
try:
print(l[i])
except IndexError as e:
print(e)
```
%%%% Output: stream
list index out of range
%% Cell type:markdown id: tags:
Remark: never use
```python
except:
```
It means "except all errors and exceptions". A user Control-C is an exception (`KeyboardInterrupt`) so it would be caught and have no effect.
%% Cell type:markdown id: tags:
## Full syntax
```python
try:
...
except <exception1> as e1:
...
except <exception2> as e2:
...
else:
...
finally:
...
```
[Non exhaustive error list](https://docs.python.org/3/library/exceptions.html):
- ArithmeticError
- ZeroDivisionError
- IndexError
- KeyError
- AttributeError
- IOError
- ImportError
- NameError
- SyntaxError
- TypeError
%% Cell type:markdown id: tags:
### Do it yourself:
For each line of this string, append a variable of correct type in a list (i.e. "hello" should stay hello,
2 should become an int and 1.5 a float). Do it by catching errors.
**Hints:**
- int("a") and float("a") raise ValueError
- the above str_variable can be split using "\n"
%% Cell type:code id: tags:
``` python
str_variables = """hello
1.5
2"""
the_list_you_should_get = ["hello", 1.5, 2]
```
%% Cell type:markdown id: tags:
#### A possible solution:
%% Cell type:code id: tags:
``` python
split_list = []
for value in str_variables.split("\n"):
try:
value = int(value)
except ValueError:
print(value, "is not a int")
try:
value = float(value)
except ValueError:
print(value, 'is not a float')
split_list.append(value)
print(split_list)
```
%%%% Output: stream
hello is not a int
hello is not a float
1.5 is not a int
['hello', 1.5, 2]
......
......@@ -68,17 +68,6 @@
"\n",
"def print_variables():\n",
" print(f'in function print_variables: myvar0 = {myvar0}; myvar1 = {myvar1}')\n",
"\n",
"print('in util.py, __name__ =', __name__)\n",
"# __name__ is a special variable always defined.\n",
"# its value depends on how the file is called (directly executed or imported)\n",
"if __name__ == '__main__':\n",
" # this code is executed only in the file is directly executed\n",
" print('the module util.py has been directly executed')\n",
" print_variables()\n",
" print('end of util.py')\n",
"else:\n",
" print('the module util.py has been imported')\n",
"\n"
]
}
......@@ -97,11 +86,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"begin of util.py\n",
"in util.py, __name__ = __main__\n",
"the module util.py has been directly executed\n",
"in function print_variables: myvar0 = 0; myvar1 = 1\n",
"end of util.py\n"
"begin of util.py\n"
]
}
],
......@@ -140,12 +125,6 @@
"myvar1 += 100\n",
"print(f'in prog.py, util.myvar0 = {util.myvar0}; myvar1 = {myvar1}')\n",
"print_variables()\n",
"\n",
"print('in prog.py, __name__ =', __name__)\n",
"# __name__ is a special variable always defined.\n",
"# its value depends on how the file is called (directly executed or imported)\n",
"if __name__ == '__main__':\n",
" print('end of prog.py')\n",
"\n"
]
}
......@@ -165,12 +144,8 @@
"output_type": "stream",
"text": [
"begin of util.py\n",
"in util.py, __name__ = util\n",
"the module util.py has been imported\n",
"in prog.py, util.myvar0 = 100; myvar1 = 101\n",
"in function print_variables: myvar0 = 100; myvar1 = 1\n",
"in prog.py, __name__ = __main__\n",
"end of prog.py\n"
"in function print_variables: myvar0 = 100; myvar1 = 1\n"
]
}
],
......@@ -178,6 +153,85 @@
"run ../pyfiles/example0/prog.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Warning: files imported more than once are executed only once per process."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Multi-file program (example with 2 files) and imports\n",
"\n",
"`if __name__ == \"__main__\": ...`\n",
"\n",
"- ../pyfiles/example1/util.py\n",
"- ../pyfiles/example1/prog.py"
]
},
{
"cell_type": "code",
"execution_count": 20,