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,
......
......@@ -14,7 +14,7 @@
"\n",
"Pierre Augier (LEGI), Cyrille Bonamy (LEGI), Eric Maldonado (Irstea), Franck Thollard (ISTerre), Christophe Picard (LJK), Loïc Huder (ISTerre)\n",
"\n",
"# More standard types and basic statements"
"# Standard types and basic statements"
]
},
{
......@@ -25,7 +25,104 @@
}
},
"source": [
"## More on standard types\n",
"## Function calls\n",
"\n",
"There are [built-in functions](https://docs.python.org/3/library/functions.html) and the developers can of course define other functions. To call a function:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"print(\"hello\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some functions return a result."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"round(1.2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's common to store the result in a variable:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"my_var = round(1.2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"which can then be used:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"print(my_var)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Few standard types\n",
"\n",
"- Simple types (`int`, `float`, `bool`, `complex`)\n",
"- Standard type `str`\n",
......@@ -2486,7 +2583,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.7.3"
}
},
"nbformat": 4,
......
......@@ -454,7 +454,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 7,
"metadata": {},
"outputs": [
{
......@@ -480,29 +480,27 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"das f [17, 4]\n",
"[17] 2\n"
"[4] 2\n"
]
}
],
"source": [
"number = 2\n",
"mylist = [17]\n",
"mylist = []\n",
"\n",
"def my_strange_append_square(mylist, number):\n",
" # new function namespace with names \"mylist\" and \"number\"\n",
" number = number**2\n",
" mylist.append( number)\n",
" print(\"das f\", mylist)\n",
" \n",
"my_strange_append_square(mylist[:], number)\n",
"my_strange_append_square(mylist, number)\n",
"print(mylist, number)"
]
},
......@@ -811,29 +809,13 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1]\n",
"[1, 1]\n",
"[1, 1, 1]\n",
"[1, 1, 1, 1]\n"
]
}
],
"outputs": [],
"source": [
"def do_not_use_mutable_object_for_default_arg(l=[]):\n",
" l.append(1)\n",
" print(l)\n",
"\n",
"do_not_use_mutable_object_for_default_arg()\n",
"do_not_use_mutable_object_for_default_arg()\n",
"do_not_use_mutable_object_for_default_arg()\n",
"do_not_use_mutable_object_for_default_arg()"
" print(l)"
]
},
{
......@@ -1044,103 +1026,6 @@
"all the elements of the list by the number. If not set, number is defaulted to 2."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stderr",