Commit 5396f64d authored by Loic Huder's avatar Loic Huder
Browse files

Typo correction in pres01_intro_language.ipynb

parent d04a7363
Pipeline #31881 passed with stage
in 1 minute and 2 seconds
......@@ -410,7 +410,7 @@
"source": [
"## Do it yourself: PEP8 checker with Spyder editor\n",
"\n",
"In Spyder, open the file `./pyfiles/wrong.py` and see what append. \n",
"In Spyder, open the file `./pyfiles/wrong.py` and see what happens. \n",
"\n",
"Activate the \"Real time code style analysis (PEP 8)\" (Preferences -> Editor -> Code Introspection/Analysis).\n",
"\n",
......
%% 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
%% 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
- 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:
## 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.
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.
%% 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/)
- 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)
%% 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:
## 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/)
- 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)
- Errors should never pass silently
- Multi-paradigm (sequential, object-oriented, functional)
- "Batteries Included": [the standard library](https://docs.python.org/3/tutorial/stdlib.html)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment