Skip to content

Instantly share code, notes, and snippets.

@balouf
Created October 13, 2021 08:34
Show Gist options
  • Save balouf/c7a62b4784e2c34146efb3d071948659 to your computer and use it in GitHub Desktop.
Save balouf/c7a62b4784e2c34146efb3d071948659 to your computer and use it in GitHub Desktop.
first_notebook.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "# Python 101"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "## Introduction"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "# Why Python?"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- A very popular language (on the podium, sometimes #1)\n- Easy to learn:\n - `1+1`\n - `print(\"Hello World\")`\n- Powerful to master:\n - Build complex packages\n - Publish them!\n- Huge ecosystem of packages and stackoverflow questions\n - Including all ML stuff (Pytorch)\n - Also some esoteric stuff"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "# Why NOT Python?"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- Slower than C (but you can learn to mitigate)\n- I need a very specific feature not available (rare but may occur)\n- I am fully happy with language X (no problem!)"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "# Python 2.X vs Python 3.Y"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- Big changes have been introduced in Python 3\n- Many legacy softwares have stayed 2.X for simplicity\n- As a beginner in Python:\n - Use Python 3.X. Period.\n - Avoid to be involved in any 2.X project if you can.\n - Be careful with 2.X solutions in stackoverflow\n"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "# How to program in Python?"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Very roughly, you have three main ways to use Python:\n- Directly inside a Python interpreter\n - Check something, make a one-liner\n- Write a python file (.py) that contains instructions to be executed\n - All-purposes, from small scripts to huge packages\n- Inside an interactive Python notebook (.ipynb)\n - Ideal to create new code and play with it"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "# How to program in Python?"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "To do python, you need:\n- A Python distribution\n- IDE for your notebooks / python files\n\nMany (many many) possibilities available. We focus on:\n- Jupyter Notebook\n- PyCharm"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "## Installation"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "# The Anaconda distribution"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "https://www.anaconda.com/products/individual\n- Pros:\n - All-in-one\n - Very easy to install\n - Can be installed on user account (highly recommended for beginners)\n- Cons:\n - 400 Mo\n - Easy to break ;)\n - Need to re-install for changing Python version"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "# Installing Anaconda"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- Tip #1: no space in the install directory\n- Tip #2: adding Anaconda to Path is not recommended, but it helps!\n- When you’re done, start an interpreter\n - `1+1`\n - `print(\"Hello World\")`\n- Optional: tour of the Anaconda Navigator\n - Possibly better for beginners?\n - Bad heaviness / usefulness trade-off"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "# Updating"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- Update is tricky:\n - Necessary to get latest features\n - Easy to inadvertently break things\n - Safest moment to update is on a fresh install!\n- Multiple ways to update/install packages in Python\n - Conda: the Anaconda internal package manager\n - Pip: universal package manager\n - ...\n- My rule of thumb:\n - Conda-related stuff (e.g. notebooks) -> conda\n - All the rest -> pip if possible"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "# Update intructions"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- `conda update conda`\n- `conda update --all`\n\nWhen ready, you can start your first notebook:\n\n`jupyter notebook`"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "## Your first notebook"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "A notebook is made of cells. A cell is a unit of content."
},
{
"metadata": {},
"cell_type": "raw",
"source": "This is a raw Cell."
},
{
"metadata": {
"slideshow": {
"slide_type": ""
}
},
"cell_type": "markdown",
"source": "This is a *markdown* cell."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T19:34:42.895182Z",
"start_time": "2021-10-12T19:34:42.868203Z"
},
"trusted": false
},
"cell_type": "code",
"source": "# This is a code cell\nprint(\"Hello world!\")",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "### Navigation and shortcuts"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Jupyter has two main modes:\n- Command mode to navigate and manipulate entire cells;\n- Edit mode to change the content of a cell."
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Recognize and switch mode:\n- Command mode (blue box): Select a cell (e.g. with arrows) and hit enter to switch to edit mode.\n- Edit mode (green box): Hit Esc to switch to command mode."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Shortcuts:\n- hit h when in command mode to see them (most are for command mode).\n- recommendation: try them all at least once!"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "### Raw cells (shortcut `r` in command mode)"
},
{
"metadata": {},
"cell_type": "raw",
"source": "You can ignore them for now (most users never need to use them)."
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "### Markdown cell (shortcut `m` in command mode)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Markdown cells are used to enter all kind of contents but actual python code with a simple and nice formatting system."
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "- This is a list\n 1. This is a numbered sublist\n 1. This is another numbered item. The actual numbers are not used.\n- This is **bold**, this is *italic*, this is ***both***.\n- You can write nice LaTeX formula in Markdown Cells, e.g. $\\int_2^3 x dx =\\frac{5}{2}$.\n- URL are automatically linked, like in https://www.google.com\n- Many other features that you will learn as you use it."
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Recommendation: when writing a plain .py file, it is a good practice to describe what you do with *comments*. On notebook, it is preferred to use Markdown cells. This is especially nice when you implement some math stuff."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Headings cells are Markdown cells that start with one or more #. They translate into `<h1>`, `<h2>`... HTML structure."
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "# First level heading"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### Fourth level heading"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "##### Fifth level heading"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "###### Last (sixth) level heading"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "### Code cell (shortcut `y` in command mode)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Code cells are where you actually write Python code. Each code cell is made of two parts:\n- input: the code to execute\n- output: the result (if any)"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "To execute a code cell:\n- click the *play* icon on the left of the cell\n- ctrl+enter executes the cell (goes command)\n- shift+enter executes the cell and move to the next one (goes command)\n- alt+enter executes the cell and create a new code cell (edit mode)"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:02:31.265347Z",
"start_time": "2021-10-12T20:02:31.254415Z"
},
"slideshow": {
"slide_type": "subslide"
},
"trusted": false
},
"cell_type": "code",
"source": "1+1",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:02:27.612670Z",
"start_time": "2021-10-12T20:02:27.596217Z"
},
"trusted": false
},
"cell_type": "code",
"source": "print(\"Hello World!\")",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Important feature: all cells in a given notebook share the same space (Kernel). This allows to build your code one step at a time"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:04:12.112142Z",
"start_time": "2021-10-12T20:04:12.100677Z"
},
"slideshow": {
"slide_type": ""
},
"trusted": false
},
"cell_type": "code",
"source": "x = 2",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:04:16.317605Z",
"start_time": "2021-10-12T20:04:16.309616Z"
},
"trusted": false
},
"cell_type": "code",
"source": "y = 3",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:04:58.989804Z",
"start_time": "2021-10-12T20:04:58.968143Z"
},
"trusted": false
},
"cell_type": "code",
"source": "x+y",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Notes :\n\n- The cell/kernel menu allow to execute multiple/all cells of a notebook, reset your notebook...\n- Each notebook has its own kernel"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "### First steps in Python"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Python uses building blocks that are very similar to what you find in other languages: if, then, for, while... and print!"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T21:23:13.091311Z",
"start_time": "2021-10-12T21:23:13.067376Z"
},
"trusted": false
},
"cell_type": "code",
"source": "name = \"Python Workshop\"\nprint(\"Hello, \"+ name + \"!\")",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "In most languages, the keyword **end** is used to tell when a loop/condition/definition ends.\n\nOr some curly brackets.\n\nIn Python, there is no **end**. Instead, the ranges are defined according to indentation."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T21:30:06.599671Z",
"start_time": "2021-10-12T21:30:06.578395Z"
},
"slideshow": {
"slide_type": "subslide"
},
"trusted": false
},
"cell_type": "code",
"source": "# In most languages, indentation is optional and only for lisibility\nx = 0\nfor i in range(5):\n x = x + i\n print(\"x is \"+str(x))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T21:30:16.318091Z",
"start_time": "2021-10-12T21:30:16.291552Z"
},
"trusted": false
},
"cell_type": "code",
"source": "# In python, indentation (hence lisibility) is part of the language itself!\n# The price: indentation errors translate into code errors\nx = 0\nfor i in range(5):\n x = x + i\nprint(\"x is \"+str(x))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "When you make a mistake, you get some error message that (may?) help you."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T21:30:19.290312Z",
"start_time": "2021-10-12T21:30:19.239216Z"
},
"trusted": false
},
"cell_type": "code",
"source": "print(\"This line is correct\")\nfor i in range(-3, 3):\n inv_i = 1/i\n print(f\"The inverse of {i} is {inv_i}\")\nprint(\"Will I get there?\")",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Defining functions is performed with **def**. "
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T21:21:49.313073Z",
"start_time": "2021-10-12T21:21:49.268373Z"
},
"trusted": false
},
"cell_type": "code",
"source": "def my_division(x, y):\n return x / y\nmy_division(10, 2)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "You can assign default values, which allows highly flexible function calls."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T21:27:57.811460Z",
"start_time": "2021-10-12T21:27:57.779934Z"
},
"trusted": false
},
"cell_type": "code",
"source": "def my_division(x=10, y=2):\n return x / y\nprint(my_division(10, 2))\nprint(my_division(7))\nprint(my_division(y=4))\nprint(my_division(y=3, x=21))",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Packages can offer additional features. Many useful packages are shipped with Anaconda."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T21:41:31.806035Z",
"start_time": "2021-10-12T21:41:31.315333Z"
},
"trusted": false
},
"cell_type": "code",
"source": "import numpy as np # numerical computation package. Very useful so we shorten its name as np\nfrom matplotlib import pyplot as plt # pyplot is a submodule (part of a module) to easily display figures\nx = np.linspace(-4, 4)\nplt.plot(x, np.sin(x), label='sin(x)')\nplt.legend()\nplt.show()",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "If you need another package, follows the package instructions (prefer `pip` if available).\nhttps://balouf.github.io/smup/installation.html"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "### Magics"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Magics are used at the start of a cell or a line to change the way the code cell is interpreted. There are a lot of them by default (and a lot that can be added)."
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Time it... to make stats about the execution time."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:53:56.502416Z",
"start_time": "2021-10-12T20:53:54.196177Z"
},
"trusted": false
},
"cell_type": "code",
"source": "%timeit 6*7",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Time it is useful for fast benchmarking."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:21:45.502247Z",
"start_time": "2021-10-12T20:21:39.028130Z"
},
"trusted": false
},
"cell_type": "code",
"source": "import numpy as np\n%timeit x = np.sum([np.random.rand()**2 for _ in range(10000)])",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:22:28.663652Z",
"start_time": "2021-10-12T20:22:20.470206Z"
},
"trusted": false
},
"cell_type": "code",
"source": "%timeit x = np.sum(np.random.rand(10000)**2)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Another one: write some javascript."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:18:37.444700Z",
"start_time": "2021-10-12T20:18:37.425570Z"
},
"trusted": false
},
"cell_type": "code",
"source": "%%js\nif (true) alert(\"This is a javascript cell\")",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "### Extensions"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Jupyter extensions are not mandatory but they can facilitate your life. \n- Once installed, they are managed in the Nbextensions tab\n- You may have to close/reopen a notebook to see changes"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "Installation:\n- `conda install -c conda-forge jupyter_contrib_nbextensions`\n- ? (conda install -c conda-forge jupyter_nbextensions_configurator)\n- ? jupyter contrib nbextension install --user\n- `conda install -c conda-forge rise` (bonus)"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "#### Execution time"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Automatic benchmark (not as precise as timeit) AND date of last execution."
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2021-10-12T20:27:00.746534Z",
"start_time": "2021-10-12T20:27:00.731790Z"
},
"trusted": false
},
"cell_type": "code",
"source": "x = np.sum(np.random.rand(10000)**2)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "#### Scratchpad (ctr+b)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "When you want to test something in a hurry."
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "#### Table of contents"
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "#### Spellchecker"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "I don't neede that."
},
{
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"cell_type": "markdown",
"source": "#### Others"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- Gist-it\n- Rise\n- Split Cells\n- Initialization cells\n- ..."
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "## Jupyter Lab"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "A notebook file (`.ipynb`) has a simple structure that can be opened/used in many contexts:\n- Jupyter Lab (the replacement for jupyter notebook).\n- Colaboratory (online notebooks)\n- Github (display only)\n- PyCharm (although PC main focus is `.py` files)"
},
{
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"cell_type": "markdown",
"source": "## PyCharm"
}
],
"metadata": {
"celltoolbar": "Slideshow",
"hide_input": false,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.7",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "first_notebook.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment