{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter notebooks are extremely cool ways to demo your code or work interactively. They are separated into cells, just like Mathematica.\n", "\n", "To...\n", "* **Close a notebook**: File > \"Close and halt\". Do NOT close the tab! Python will still run in the background.\n", "* **Edit text**: Double-click a cell. Shift+Enter when done.\n", "* **Evaluate code in a cell**: Shif+Enter.\n", "* **Create a new cell above or below**: ESC+a, ESC+b\n", "* **See all the keyboard shortcuts**: ESC+h\n", "\n", "See the Jupyter cheatsheet for many more commands!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Cell types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can control the type of cell in Cell -> Cell Type, and choose whether your cell is\n", "- text\n", "- code \n", "\n", "Text cells can be formatted following Markdown conventions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 0\n", "\n", "0.1: Open the keyboard shortcuts (see bullet list above). What are the two different keyboard input modes?\n", "\n", "0.2: Create a new cell below this one, and change it from a code cell to a text cell. Write a cool message to yourselves, using some markdown formatting." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Some code and cool things" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 'Hello, world!'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the **output of a cell** in the last line is automatically printed if it's a variable or unassigned output:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 4\n", "2 * x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a bunch of **\"magic\" commands** that can really up your game." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%lsmagic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, let's test how long a silly function takes..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "def silly_function():\n", " np.random.randint(0, high=100, size=int(1e6))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "silly_function()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Auto-complete suggestion** Place the cursor at the end of `np.ar` below and hit *TAB* on the keyboard" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.ar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "you should get at list of all the function in numpy starting with `ar`\n", "\n", "**Get function documentation** Place the cursor inside the brackets and hit and hold *SHIFT* and then hit *TAB*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "you should get a window showing the first part of the documentation string for the function. Press the \"plus\" to extend the view or the \"arrow\" to make it a full width window. The documentation string can also be written to an output cell as shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(np.arange)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define $x$ and $y$ below such that $x$ is a numpy array from 1 to 9 (inclusive) and $y=1/x$. Then, run your cell and the next cell and look at the resulting plot. Does it make sense?\n", "\n", "__HINT__: Use the documentation \"arange\" from above to define $x$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "# x = ???\n", "# y = ???" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "fig, ax = plt.subplots(1, 1, figsize=(6, 3))\n", "ax.scatter(x, y)\n", "ax.set(title='Hello, world!', xlabel='x', ylabel='y = 1/x');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# When you're done\n", "\n", "1. Close the notebook by hitting File > \"Close and halt\".\n", "2. In the \"Home page\" tab, click \"Quit\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.11" } }, "nbformat": 4, "nbformat_minor": 2 }