<jupyter_start><jupyter_text>Jupyter Notebooks==================A notebook consists in a set of cells. These cells are interpreted either as text instruction (i.e. **markdown**) or as Python **code**.* Each cell can be edited using ``[Enter]`` key (i.e. *edit mode*). To return to the *navigation mode*, use the ``[Esc]`` key.* To switch change a cell to a Python code cell, go in navigation mode and the press ``[y]`` key. Similary, to change a cell to a markdown cell, press the ``[m]`` key.* You can run a cell by pressing ``[shift] + [Enter]`` or by pressing the "play" button in the menu.![](../images/ipython_run_cell.png)* You can get help on a function or object by pressing ``[shift] + [tab]`` after the opening parenthesis ``function(``![](../images/ipython_help-1.png)* You can also get help by executing ``function?``![](../images/ipython_help-2.png)Some useful keyboard shortcut (*you need to be in navigation mode*):* Add a cell above the current cell -> press ``[a]``* Add a cell below the current cell -> press ``[b]``* Select several cells -> hold ``[Shift]`` + arrow ``[up]`` or ``[down]``* Remove the selected cell(s) -> press ``[dd]``* Copy the selected cell(s) -> press ``[c]``* Cut the selected cell(s) -> press ``[x]``* Paste the copy/cut cell(s) -> press ``[v]``  Introduction to Python We introduce here the Python language. Only the bare minimumnecessary for getting started with Numpy and Scipy is addressed here.To learn more about the language, consider going through theexcellent tutorial http://www.scipy-lectures.org/intro/index.html. Dedicated booksare also available, such as http://www.diveintopython.net/.Python is a **programming language**, as are C, Fortran, BASIC, PHP,etc. Some specific features of Python are as follows:* an *interpreted* (as opposed to *compiled*) language. Contrary to e.g.C or Fortran, one does not compile Python code before executing it. Inaddition, Python can be used **interactively**: many Pythoninterpreters are available, from which commands and scripts can beexecuted.* a free software released under an **open-source** license: Python canbe used and distributed free of charge, even for building commercialsoftware.* **multi-platform**: Python is available for all major operatingsystems, Windows, Linux/Unix, MacOS X, most likely your mobile phoneOS, etc.* a very readable language with clear non-verbose syntax* a language for which a large variety of high-quality packages areavailable for various applications, from web frameworks to scientificcomputing.* a language very easy to interface with other languages, in particular Cand C++.* Some other features of the language are illustrated just below. Forexample, Python is an object-oriented language, with dynamic typing(the same variable can contain objects of different types during thecourse of a program).See https://www.python.org/about/ for more information aboutdistinguishing features of Python.  0. Hello worldyou type in the cells, execute commands with shift + Enteryou can add whatever you want<jupyter_code>print("Hello world")<jupyter_output><empty_output><jupyter_text>1. Basic types 1.1. Integers<jupyter_code>1+1<jupyter_output><empty_output><jupyter_text>We can assign values to variables with `=`<jupyter_code>a = 4
type(a)<jupyter_output><empty_output><jupyter_text>Note that one does not declare the type of a variable before assigning its value. In C, conversely, one should write:```Cint a = 4;```  1.2 FloatsThere exists a floating point type that is created when the variable has decimal values:<jupyter_code>c = 2.1
c
type(c)<jupyter_output><empty_output><jupyter_text>This gives a different result when using `Python 2` and `Python 3`<jupyter_code>1 / 2, 1. /2<jupyter_output><empty_output><jupyter_text>NB: this uses an object that we'll discover later, the `tuple`<jupyter_code>t = 2*3, 4.
type(t)<jupyter_output><empty_output><jupyter_text>1.3. BooleanSimilarly, boolean types are created from a comparison:<jupyter_code>3 > 4
test = (3 > 4)
type(test)<jupyter_output><empty_output><jupyter_text>NB: A Python shell can therefore replace your pocket calculator, with the basic arithmetic operations ``+``, ``-``, ``*``, ``/``, ``%`` (modulo) natively implemented  1.4. Type conversion (casting)<jupyter_code>a = 1
type(a)
b = float(a)
type(b)
a, b
a == b
(1.0 + 0.1 - 1.0) == (1.0 - 1.0 + 0.1)
abs((1.0 + 0.1 - 1.0) - (1.0 - 1.0 + 0.1)) < 1e-10<jupyter_output><empty_output><jupyter_text>2. ContainersPython provides many efficient types of containers, in which collections of objects can be stored. The main ones are `list`, `tuple`, `set` and `dict`  2.1. Tuples<jupyter_code>tt = ('truc', 3.14)
tt
tt_list = ['truc', 3.14]
tt_list
tt_list.sort()
tt[0]<jupyter_output><empty_output><jupyter_text>You can't change a tuple, we say that it's *immutable*<jupyter_code>tt[0] = 1
a = 2
a_list = [ a, 3]
a_list
a = 3
a_list<jupyter_output><empty_output><jupyter_text>2.1. ListsA list is an ordered collection of objects, that may have different types. For example:<jupyter_code>colors = ['red', 'blue', 'green', 'black', 'white']
type(colors)<jupyter_output><empty_output><jupyter_text>Indexing: accessing individual objects contained in the list::<jupyter_code>colors[2]<jupyter_output><empty_output><jupyter_text>WARNING: **Indexing starts at 0** (as in C), not at 1 (as in Fortran or Matlab).Counting from the end with negative indices:<jupyter_code>colors[-2]
colors[10]
len(colors)
colors[1] = 'purple'<jupyter_output><empty_output><jupyter_text>Afficher la liste<jupyter_code># Afficher la liste
colors
colors.sort()
colors
colors2 = colors
colors2.sort(reverse = True)
colors2
colors
colors2 = colors.copy()
colors.sort()
colors
colors2
colors3 = list(colors2)
colors3.sort()
colors2<jupyter_output><empty_output><jupyter_text>2.2. Slicing: obtaining sublists of regularly-spaced elements<jupyter_code>colors
colors[1:5:2]<jupyter_output><empty_output><jupyter_text>**Slicing syntax**: ``colors[start:stop:stride]``NB: All slicing parameters are optional<jupyter_code>colors
colors[3:]
colors[:3]
colors[::2]
colors[2:0:-1]
colors[7:8]<jupyter_output><empty_output><jupyter_text>2.3. StringsDifferent string syntaxes (simple, double or triple quotes):<jupyter_code>s = 'asdfdfdf'
type(s)
s = "asdfdfd"
s = """asdfadsf
asdfdf 
asdfdf 
"""
s
print(s)
len(s)
list(s)
s.strip().split()
s_list = s.strip().split("\n")
s2_list = s_list
s_list[2]
s_list[2:3]
s[3:10]
s + ' coucou'
s.title()<jupyter_output><empty_output><jupyter_text>2.4 Dictionaries A dictionary is basically an efficient table that **maps keys to values**. It is an **unordered** container<jupyter_code>tel = {'emmanuelle': 5752, 'sebastian': 5578}
tel
tel['emmanuelle']
tel['francis']
tel['francis'] = 5919
tel
tel.keys()
tel.values()
'francis' in tel<jupyter_output><empty_output><jupyter_text>It can be used to conveniently store and retrieve valuesassociated with a name (a string for a date, a name, etc.). Seehttps://docs.python.org/tutorial/datastructures.htmldictionariesfor more information.NB: A dictionary can have keys (resp. values) with different types:<jupyter_code>d = {'a':1, 'b':2, 3: 'asdf'}
d
del d['a']
tel = {'emanuelle': 5752, 'sebastian' : 5578, 'francis' : 1234}
tel
tel['emanuelle'], tel['sebastian'] = tel['sebastian'], tel['emanuelle']
tel<jupyter_output><empty_output><jupyter_text>2.5 SetsA set contain is an unordered container, containing unique elements<jupyter_code>s = 'truc truc bidule truc'
set(s)
len(set(s))
set([1, 5, 2, 1, 1]).union(set([1, 2, 3]))<jupyter_output><empty_output><jupyter_text>You can use together all types together<jupyter_code>dd = {'truc': [1, 2, 3], 
      5: (1, 4, 2),
      (1, 3): set(['hello', 'world'])}
dd<jupyter_output><empty_output><jupyter_text>NB: any immutable type can be used as a key in a dict (such as a tuple)<jupyter_code>dd[[2, 3]] = 4<jupyter_output><empty_output><jupyter_text>2.6. AssigmentAssigment operator `=` in python does not do a copy. It actually works as name binding (when object are mutable).<jupyter_code>l1 = [4, 1, 3, 2, 2, 2]
l2 = l1
l1[0] = 123
l2
l2 = l1.copy()
l1[0] = -1
l2
l1.append(5)
l1
l1.pop()
l1.insert(1, 0)
l1<jupyter_output><empty_output><jupyter_text>3. Control FlowTest, loops, etc.<jupyter_code>if 2 ** 2 == 4:
    print('Obvious')
print('YES')<jupyter_output><empty_output><jupyter_text>3.1. Blocks are delimited by indentation!<jupyter_code>a = 2
if a == 1:
    print(2)
elif a == 2:
    print(1\
          )    
else:
    print(a)<jupyter_output><empty_output><jupyter_text>3.2. for/rangeIteration with an index, with a list, with many things !<jupyter_code>for i in range(4):
    print(i + 1)
print('-')
for i in range(1, 5):
    print(i)
print('-')
for i in range(1, 10, 3):
    print(i)
s
for c in s.split(" "):
    print(c)
for word in ['green', 'blue', 'yellow']:
    print('best color is', word)<jupyter_output><empty_output><jupyter_text>3.3. List Comprehensions<jupyter_code>[i ** 2 for i in range(6)]
[2 ** i for i in range(9)]<jupyter_output><empty_output><jupyter_text>3.4 While<jupyter_code>a = 10
b = 1
while b < a:
    b = b + 1
    print(b)<jupyter_output><empty_output><jupyter_text>Compute the decimals of Pi using the Wallis formula:$$\pi = 2 \prod_{i=1}^{100} \frac{4i^2}{4i^2 - 1}$$<jupyter_code>pi = 2
eps = 1e-10
dif = 2 * eps
i = 1
while(dif > eps):
    old_pi = pi
    pi *= 4 * i ** 2 / (4 * i ** 2 - 1)
    dif = pi - old_pi
    i += 1
pi<jupyter_output><empty_output><jupyter_text>3.4 zip / dict comprehension / itertools / enumerate<jupyter_code>s = "salut tintin"

{i: c for i, c in enumerate(s)}
enumerate(s)
dict(enumerate(s))
s1 = 'machinee'; s2 = 'magiques'
zip(s1, s2)
list(zip(s1, s2))
dict(zip(s1, s2))<jupyter_output><empty_output><jupyter_text>4. Defining functions 4.1. Function definitionWarning: Function blocks must be indented as other control-flow blocks.<jupyter_code>def test():
    print('in test function')
test()<jupyter_output><empty_output><jupyter_text>4.2. Return statementFunctions can *optionally* return values.Note: By default, functions return ``None``.The syntax to define a function:    * the ``def`` keyword;    * is followed by the function's **name**, then    * the arguments of the function are given between parentheses followed      by a colon.    * the function body;    * and ``return object`` for optionally returning values.<jupyter_code>def f(x):
    return x + 10
f(20)<jupyter_output><empty_output><jupyter_text>A function that returns several elements return a `tuple`<jupyter_code>def f():
    return 1, 4

f()
type(f())<jupyter_output><empty_output><jupyter_text>4.3. ParametersMandatory parameters (positional arguments)<jupyter_code>def double_it(x):
    return x * 2

double_it(3)
double_it()
def double_it(x = 2):
    return x * 2

double_it()
double_it(3)
def f(x, y=2, * , z=10):
    print(x, '+', y, '+', z, '=', 
          x + y + z)
f(5, 3, 7)
f(2)
f(5, -2)
f(5, z = -2)
f(5, z = -2, y = 3)
dd = {'y': 10, 'z': -5}
f(3, **dd)<jupyter_output><empty_output><jupyter_text>Prototype of all Python's functions is<jupyter_code>def f(*args, **kwargs):
    print('args=', args)
    print('kwargs=', kwargs)
f(1, 3)
f(3, -2, y='truc')<jupyter_output><empty_output><jupyter_text>5. Object-oriented programming (OOP)Python supports object-oriented programming (OOP). The goals of OOP are:- to organize the code, and- to re-use code in similar contexts.Here is a small example: we create a `Student` class, which is an objectgathering several custom functions (called *methods*) and variables (called *attributes*).<jupyter_code>s = 'truc'
s.title()
class Student(object):

    def __init__(self, name, age, major='computer science'):
        self.name = name
        self.age = age
        self.major = major

    def to_string(self):
        return str.join(', ', [attr + '=' + str(getattr(self, attr)) \
                    for attr in ['name', 'age', 'major']])
    
    def show(self):
        print(self.to_string())

anna = Student('anna', 23)
anna.show()<jupyter_output><empty_output><jupyter_text>**Inheritance**: MasterStudent is a Student with extra mandatory `Internship` attribute<jupyter_code>class MasterStudent(Student):
    
    def __init__(self, name, age, intership,
                 major='computer science'):
        Student.__init__(self, name, age, major)
        self.intership = intership

    def to_string(self):
        s = Student.to_string(self)
        return str.join(', ', [s, 'intership=' + self.intership])
djalil = MasterStudent('djalil', 22, 'pwc')
djalil.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14130781499202552, 'fraction_numerical': 0.024401913875598084, 'mean_word_length': 5.156602847324497, 'pattern_counts': {'":': 0, '<': 151, '<?xml version=': 0, '>': 159, 'https://': 2, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '37', 'dir': 'jupyter-structured-clean-dedup', 'id': '458060', 'license': 'CC0-1.0', 'n_tokens_mistral': 4587, 'n_tokens_neox': 4363, 'n_words': 1348, 'path': '00_python/notebook.ipynb', 'repo_name': 'glemaitre/smob_paristech_12_2018', 'size': '12540'}