EPITA 2021 MLRF practice_01-01_jupyter v2021-05-17_160644 by Joseph CHAZALON

Creative Commons License This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).

Practice session 1 part 1: Jupyter

This first session is based on a Jupyter notebook, like the future ones. We this this is a great format because:

It may be a good idea to spent 10 minutes learning a few tricks about Jupyter. We'll focus on:

  1. manipulating cells,
  2. running code,
  3. writing comments,
  4. and glimpse the power of iPython's magic…

You can safely skip this session if you already have experience using Jupyter.

Manipulating cells

Jupyter has two modes:

  1. Command Mode (press Esc to enable)
  2. Edit Mode (press Enter to enable)

If you are using Chrome (which exhibited better compatibility in our tests), you can toggle the command palette by pressing Ctrl+Shift+P at any time.

work **Press Ctrl+Shift+P now to reveal the command palette and type `shortcut` to look for keyboard shortcuts.**

You can change how cells are interpreted. In particular, 2 cell types are of interest:

  1. code (Esc+y) ← use it to write functions and call them
  2. markdown (Ctrl+m) ← use it to take notes
work **Focus this cell and change its type. Notice how syntax highlighting is changing.**

You can also insert, merge, copy, paste, delete, move cells.

work **Check the keyboard shortcut by pressing h in command mode, then look for some shortcuts.** **Make sure to remember the shortcuts for inserting above (a), below (b) and delete (d+d) because you will use them all the time.**

Running cells

Running a cell is so easy!

  1. First enter edit mode (Enter)
  2. Then use either:
    • Ctrl+Enter to run and focus this cell in command mode;
    • Shift+Enter to run the current cell and focus the next one in command mode;
    • Alt+Enter to run the current cell, insert a new one below, and enter edit mode for the new cell.
work **Now focus the cell below, change its mode to code, then run it.**
print('$qsD!Me+nsodd= 1l lre3W$'[-2:2:-2])

Writing comments

Writing comments is really easy too: just change the cell type to markdown and write text in ReStructured format.

It is as easy as $1 + 1 = 3$!

Or even:

$$(f * g)(t) \triangleq\ \int_{-\infty}^\infty f(\tau) g(t - \tau) \, d\tau$$
work **Edit and correct the errors in the previous cell.**

iPython's magic

Jupyter is a tool to display notebooks and run code in kernels.

It uses iPython to interactively run Python code.

iPython provides much more features than the default Python interpreter, among which:

work **Use the `%quickref` magic to have a look at all available features.**
work **Capture the list of notebook files in the current folder to a custum variable using shell escape `!`.**

Let's complete this preambule by measuring how much time is necessary to perform a simple computation.

Here is a simple function which return a positive integer for any positive integer > 0.

work **Now measure the time required to compute the value associated to some arbitrary number (like $63,728,127$).**

Finally, let's try to find which numbers in $[1, 100000]$ produces the greatest output.

work **Now measure the time required to compute the result for the following cell.**

And, to complete this short list of tips and tricks, my favorite: the online help. You can add ? at the beginning or the end of a line to display the help about the related symbol.

work **Try to display some help about a well-known function.** *Tip: using two `?`s should provide more help, if available.*

You can also use wildcards to display all matching symbols in the current namespace!

This can be particularly useful with OpenCV to locate some functions.

You can use this trick like this:

Job done!

Now you're ready to move on to the next stage: Sharpening your NumPy-fu skills.