Commit 1e750f37 authored by paugier's avatar paugier
Browse files

Rework on first notebooks before training HPC

parent ee25f4a5
......@@ -310,7 +310,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.3"
}
},
"nbformat": 4,
......
......@@ -34,154 +34,7 @@
"\n",
"- Automatic memory management\n",
"\n",
"- Dynamically strongly typed (see next slides)\n",
"\n",
"- Gradual learning curve\n",
"\n",
"- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)\n",
"\n",
"- Very clean and readable\n",
"\n",
"- Indentation defines the blocks\n",
"\n",
"- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/) (see next slides)\n",
"\n",
"- 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)\n",
"\n",
"- Errors should never pass silently\n",
"\n",
"- Multi-paradigm (sequential, object-oriented, functional)\n",
"\n",
"- \"Batteries Included\": [the standard library](https://docs.python.org/3/tutorial/stdlib.html)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## PEP8 (Python Extension Proposal)\n",
"\n",
"https://www.python.org/dev/peps/pep-0008/\n",
"\n",
"- Code layout\n",
"- Imports\n",
"- Whitespace in expressions and statements\n",
"- Comments\n",
"- Documentation strings\n",
"- Naming conventions\n",
"- Programming recommendations"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## PEP8: examples of bad and good style practices"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# bad (spaces between operator)\n",
"number=0\n",
"# ok\n",
"number = 0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# bad (indentation with 2 spaces, has to be 4)\n",
"if number == 0:\n",
" number = 1\n",
"\n",
"# ok\n",
"if number == 0:\n",
" number = 1 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# bad (space after ,)\n",
"mylist = [1,2,3]\n",
"\n",
"# ok\n",
"mylist = [1, 2, 3]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Keywords"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Here is a list of the Python keywords. Enter any keyword to get more help.\n",
"\n",
"False def if raise\n",
"None del import return\n",
"True elif in try\n",
"and else is while\n",
"as except lambda with\n",
"assert finally nonlocal yield\n",
"break for not \n",
"class from or \n",
"continue global pass \n",
"\n"
]
}
],
"source": [
"help(\"keywords\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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)!"
"- Dynamically strongly typed"
]
},
{
......@@ -386,6 +239,142 @@
"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",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Characteristics of the Python language\n",
"\n",
"- Open-source language, interpreters and ecosystem\n",
"\n",
"- Interpreted (but there are tools to compile Python code)\n",
"\n",
"- Automatic memory management\n",
"\n",
"- Dynamically strongly typed\n",
"\n",
"- Gradual learning curve\n",
"\n",
"- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)\n",
"\n",
"- Very clean and readable\n",
"\n",
"- Indentation defines the blocks\n",
"\n",
"- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## PEP8 (Python Extension Proposal)\n",
"\n",
"https://www.python.org/dev/peps/pep-0008/\n",
"\n",
"- Code layout\n",
"- Imports\n",
"- Whitespace in expressions and statements\n",
"- Comments\n",
"- Documentation strings\n",
"- Naming conventions\n",
"- Programming recommendations"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## PEP8: examples of bad and good style practices"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# bad (spaces between operator)\n",
"number=0\n",
"# ok\n",
"number = 0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# bad (indentation with 2 spaces, has to be 4)\n",
"if number == 0:\n",
" number = 1\n",
"\n",
"# ok\n",
"if number == 0:\n",
" number = 1 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# bad (space after ,)\n",
"mylist = [1,2,3]\n",
"\n",
"# ok\n",
"mylist = [1, 2, 3]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Characteristics of the Python language\n",
"\n",
"- Open-source language, interpreters and ecosystem\n",
"\n",
"- Interpreted (but there are tools to compile Python code)\n",
"\n",
"- Automatic memory management\n",
"\n",
"- Dynamically strongly typed\n",
"\n",
"- Gradual learning curve\n",
"\n",
"- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)\n",
"\n",
"- Very clean and readable\n",
"\n",
"- Indentation defines the blocks\n",
"\n",
"- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -427,6 +416,125 @@
"\n",
"Correct the file to remove the warnings."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Characteristics of the Python language\n",
"\n",
"- Open-source language, interpreters and ecosystem\n",
"\n",
"- Interpreted (but there are tools to compile Python code)\n",
"\n",
"- Automatic memory management\n",
"\n",
"- Dynamically strongly typed\n",
"\n",
"- Gradual learning curve\n",
"\n",
"- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)\n",
"\n",
"- Very clean and readable\n",
"\n",
"- Indentation defines the blocks\n",
"\n",
"- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)\n",
"\n",
"- 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)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Keywords"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Here is a list of the Python keywords. Enter any keyword to get more help.\n",
"\n",
"False def if raise\n",
"None del import return\n",
"True elif in try\n",
"and else is while\n",
"as except lambda with\n",
"assert finally nonlocal yield\n",
"break for not \n",
"class from or \n",
"continue global pass \n",
"\n"
]
}
],
"source": [
"help(\"keywords\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Characteristics of the Python language\n",
"\n",
"- Open-source language, interpreters and ecosystem\n",
"\n",
"- Interpreted (but there are tools to compile Python code)\n",
"\n",
"- Automatic memory management\n",
"\n",
"- Dynamically strongly typed\n",
"\n",
"- Gradual learning curve\n",
"\n",
"- A philosophy: the [Zen of Python](https://www.python.org/dev/peps/pep-0020/)\n",
"\n",
"- Very clean and readable\n",
"\n",
"- Indentation defines the blocks\n",
"\n",
"- Style coding is important: [pep8](https://www.python.org/dev/peps/pep-0008/)\n",
"\n",
"- 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)\n",
"\n",
"- Errors should never pass silently\n",
"\n",
"- Multi-paradigm (sequential, object-oriented, functional)\n",
"\n",
"- \"Batteries Included\": [the standard library](https://docs.python.org/3/tutorial/stdlib.html)"
]
}
],
"metadata": {
......@@ -446,7 +554,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.3"
}
},
"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)
# 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)!
- Dynamically strongly typed
%% 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:
## 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](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/)
%% 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:
## 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