{ "cells": [ { "cell_type": "markdown", "id": "152e9a2c", "metadata": {}, "source": [ "# Jupyter Notebook Tutorial\n" ] }, { "cell_type": "markdown", "id": "9f97fc6d", "metadata": {}, "source": [ "**Course**: [_Analyse Numérique pour SV_](https://moodle.epfl.ch/course/info.php?id=) (MATH-2xx)" ] }, { "cell_type": "markdown", "id": "b18b793a", "metadata": {}, "source": [ "**Prof** _Simone Deparis_\n", "\n", "SSV, BA4, 2020" ] }, { "cell_type": "markdown", "id": "91c7ade4", "metadata": {}, "source": [ "Adapted from the [Jupyter tutorial](https://github.com/aparrish/rwet/blob/master/jupyter-notebook-tutorial.ipynb) by [Allison Parrish](https://www.decontextualize.com/) and the version of Prof. Felix Naef for BIO-341" ] }, { "cell_type": "markdown", "id": "3ddb9c1f", "metadata": {}, "source": [ "Jupyter Notebook gives you a convenient way to experiment with Python code, interspersing your experiments with notes and documentation. You can do all of this without having to muck about on the command line, and the resulting file can be easily published and shared with other people. In this course, I'll be using Jupyter Notebook to do in-class examples, and the notes will be made available as Jupyter Notebooks. Some of the homeworks will be assigned in the form of Jupyter Notebooks as well.\n", "\n", "A Jupyter Notebook consists of a number of \"cells,\" stacked on the page from top to bottom. Cells can have text or code in them. You can change a cell's type using the \"Cell\" menu at the top of the page; go to `Cell > Cell Type` and select either `Code` for Python code or `Markdown` for text. (You can also change this for the current cell using the drop-down menu in the toolbar.)\n", "\n", "## Text cells\n", "\n", "Make a new cell, change its type to `Markdown`, type some stuff and press `Ctrl-Enter`. Jupyter Notebook will \"render\" the text and display it on the page in rendered format. You can hit `Enter` or click in the cell to edit its contents again. Text in `Markdown` cells is rendered according to a set of conventions called Markdown. Markdown is a simple language for marking up text with basic text formatting information (such as bold, italics, hyperlinks, tables, etc.). [Here's a tutorial](http://markdowntutorial.com/). You'll also be learning Markdown in more detail in the Foundations course.\n", "\n", "## Code cells\n", "\n", "You can also press `Alt-Enter` to render the current cell and create a new cell. New cells will by default be `Code` cells. Try it now!" ] }, { "cell_type": "code", "execution_count": null, "id": "e2a014af", "metadata": {}, "outputs": [], "source": [ "print(\"This is a code cell.\")\n", "print(\"\")\n", "print(\"Any Python code you type in this cell will be run when you press the 'Run' button,\")\n", "print(\"or when you press Ctrl-Enter.\")\n", "print(\"\")\n", "print(\"If the code evaluates to something, or if it produces output, that output will be\")\n", "print(\"shown beneath the cell after you run it.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "e23b5318", "metadata": {}, "outputs": [], "source": [ "print(\"If your Python code generates an error, the error will be displayed in addition\")\n", "print(\"to any output already produced.\")\n", "\n", "1 / 0" ] }, { "cell_type": "markdown", "id": "7b492186", "metadata": {}, "source": [ "Any variables you define or modules you import in one code cell will be available in subsequent code cells. Start with this:" ] }, { "cell_type": "code", "execution_count": null, "id": "06f33261", "metadata": {}, "outputs": [], "source": [ "import random\n", "stuff = [\"cheddar\", \"daguerrotype\", \"elephant\", \"flea market\"]" ] }, { "cell_type": "markdown", "id": "9ce9e944", "metadata": {}, "source": [ "... and in subsequent cells you can do this:" ] }, { "cell_type": "code", "execution_count": null, "id": "8c81a051", "metadata": {}, "outputs": [], "source": [ "print(random.choice(stuff))" ] }, { "cell_type": "markdown", "id": "78deed4e", "metadata": {}, "source": [ "## Keyboard shortcuts\n", "\n", "As mentioned above, `Ctrl-Enter` runs the current cell; `Alt-Enter` runs the current cell and then creates a new cell. `Enter` will start editing whichever cell is currently selected. To quit editing a cell, hit `Esc`. If the cursor isn't currently active in any cell (i.e., after you've hit `Esc`), a number of other keyboard shortcuts are available to you:\n", "\n", "* `m` converts the selected cell to a Markdown cell\n", "* `b` inserts a new cell below the selected one\n", "* `x` \"cuts\" the selected cell; `v` pastes a previously cut cell below the selected cell\n", "* `h` brings up a help screen with many more shortcuts.\n", "\n", "## Saving your work\n", "\n", "Hit `Cmd-S` at any time to save your notebook. Jupyter Notebook also automatically saves occasionally. Make sure to give your notebook a descriptive title by clicking on \"Untitled0\" at the top of the page and replacing the text accordingly. Notebooks you save will be available on your server whenever you log in again, from wherever you log into the server.\n", "\n", "You can \"download\" your notebook in various formats via `File > Download as`. You can download your notebook as a static HTML file (for, e.g., uploading to a web site), or as a `.ipynb` file, which you can share with other people who have Jupyter Notebook or make available online through, e.g., [nbviewer](http://nbviewer.ipython.org/)." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" }, "unianalytics_cell_mapping": [ [ "152e9a2c", "152e9a2c" ], [ "9f97fc6d", "9f97fc6d" ], [ "b18b793a", "b18b793a" ], [ "91c7ade4", "91c7ade4" ], [ "3ddb9c1f", "3ddb9c1f" ], [ "e2a014af", "e2a014af" ], [ "e23b5318", "e23b5318" ], [ "7b492186", "7b492186" ], [ "06f33261", "06f33261" ], [ "9ce9e944", "9ce9e944" ], [ "8c81a051", "8c81a051" ], [ "78deed4e", "78deed4e" ] ], "unianalytics_notebook_id": "4aebc203-3d37-4377-b707-8d4a970f449d" }, "nbformat": 4, "nbformat_minor": 5 }