{ "metadata": { "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.9.2-final" }, "orig_nbformat": 2, "kernelspec": { "name": "python392jvsc74a57bd0a27d3f2bf68df5402465348834a2195030d3fc5bfc8e594e2a17c8c7e2447c85", "display_name": "Python 3.9.2 64-bit ('ds-3.9': conda)" } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "source": [ "# Jupyter Notebook: Some Basics\n", "\n", "This notebook attempts to illustrate some of the material covered in my related post, \"../2021/05/03/ds_basics_2/\". The post was providing a very basic introduction to the Jupyter (IPython) Notebook, as part of a possible series covering the basics of data science." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "# This is code cell\n", "x = 3\n", "x" ] }, { "source": [ "## Markdown (Text) Cell\n", "\n", "The above is a level 2 heading (2 #s). The first cell had a level 1 heading.\n", "\n", "This is a paragraph. One continuous line of text with a blank line before and after.\n", "\n", "You can mark text as *italic* by enclosing it in single asteriks (Shift 8 on the keyboard).\n", "\n", "**Bold** text is marked with two enclosing asteriks. You can do both by enclosing in ***three asteriks***.\n", "\n", "Lists are really quite simple. Just start each line with a 'dash' and a 'space'.\n", "- item 1\n", "- item 2\n", "\n", "If you'd like a numbered list, just put a number with a trailing period in front instead of a dash. In this case you need a blank line before and after the list.\n", "\n", "1. item 1\n", "1. item 1\n", "1. item 3\n", "\n", "You can mark code by enclosing it in backticks, e.g. `print(x)`.\n", "\n", "There's plenty more, check out this basic [cheat sheet](https://www.markdownguide.org/cheat-sheet/#downloads)\n", "\n", "Double-click in the cell to see the raw markdown. (Or, click in it and press `Enter`)." ], "cell_type": "markdown", "metadata": {} }, { "source": [ "# Another code cell\n", "\n", "def get_length(pt1, pt2):\n", " \"\"\"Return the distance between two points on a 2-dimensional plane.\n", " I.E. the length of the vector the two points represent\n", " pt1, pt2: tuple giving the x and y coordinates for the point, e.g. (-3, 4)\n", " \"\"\"\n", " x_diff = pt2[0] - pt1[0]\n", " y_diff = pt2[1] - pt1[1]\n", " xy_sum = x_diff**2 + y_diff**2\n", " return xy_sum**0.5\n" ], "cell_type": "code", "metadata": {}, "execution_count": 4, "outputs": [] }, { "source": [ "Try flipping the above cell between code and markdown types to see what happens. Don't forget to run the cell after switching.\n", "\n", "Done forget to try `+M` and `+Y`.\n", "\n", "And, do try using `+A` and `+B` to insert an empty cell above or below the currently selected cell.\n" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Object `sort` not found.\n" ] } ], "source": [ "# Okay let's try getting some help with a function\n", "# Say the builtin sort() function\n", "\n", "sort?\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "output_type": "stream", "text": [ "\u001b[1;31mSignature:\u001b[0m \u001b[0mt_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreverse\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mDocstring:\u001b[0m\n", "Sort the list in ascending order and return None.\n", "\n", "The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n", "order of two equal elements is maintained).\n", "\n", "If a key function is given, apply it once to each list item and sort them,\n", "ascending or descending, according to their function values.\n", "\n", "The reverse flag can be set to sort in descending order.\n", "\u001b[1;31mType:\u001b[0m builtin_function_or_method\n" ], "name": "stdout" } ], "source": [ "# Well, I guess we need a suitable object\n", "\n", "t_list = []\n", "t_list.sort?\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "output_type": "stream", "text": [ "\u001b[1;31mSignature:\u001b[0m \u001b[0mget_length\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpt1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpt2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mDocstring:\u001b[0m\n", "Return the distance between two points on a 2-dimensional plane.\n", "I.E. the length of the vector the two points represent\n", "pt1, pt2: tuple giving the x and y coordinates for the point, e.g. (-3, 4)\n", "\u001b[1;31mFile:\u001b[0m r:\\learn\\ds_intro\\\n", "\u001b[1;31mType:\u001b[0m function\n" ], "name": "stdout" } ], "source": [ "# how about the function we defined above\n", "# don't forget to make sure you run the cell with the definition before running this one\n", "\n", "get_length?\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "# This is code cell\nx = 3\nx\n" ] } ], "source": [ "print(In[1])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "3\n" ] } ], "source": [ "print(Out[1])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{1: 3}\n" ] } ], "source": [ "# There are some issues with using In and Out\n", "# Cell input/output numbers are changed whenever you change/re-run a cell\n", "\n", "print(Out)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "output_type": "stream", "text": [ "\n", "IPython's 'magic' functions\n", "===========================\n", "\n", "The magic function system provides a series of functions which allow you to\n", "control the behavior of IPython itself, plus a lot of system-type\n", "features. There are two kinds of magics, line-oriented and cell-oriented.\n", "\n", "Line magics are prefixed with the % character and work much like OS\n", "command-line calls: they get as an argument the rest of the line, where\n", "arguments are passed without parentheses or quotes. For example, this will\n", "time the given statement::\n", "\n", " %timeit range(1000)\n", "\n", "Cell magics are prefixed with a double %%, and they are functions that get as\n", "an argument not only the rest of the line, but also the lines below it in a\n", "separate argument. These magics are called with two arguments: the rest of the\n", "call line and the body of the cell, consisting of the lines below the first.\n", "For example::\n", "\n", " %%timeit x = numpy.random.randn((100, 100))\n", " numpy.linalg.svd(x)\n", "\n", "will time the execution of the numpy svd routine, running the assignment of x\n", "as part of the setup phase, which is not timed.\n", "\n", "In a line-oriented client (the terminal or Qt console IPython), starting a new\n", "input with %% will automatically enter cell mode, and IPython will continue\n", "reading input until a blank line is given. In the notebook, simply type the\n", "whole cell as one entity, but keep in mind that the %% escape can only be at\n", "the very start of the cell.\n", "\n", "NOTE: If you have 'automagic' enabled (via the command line option or with the\n", "%automagic function), you don't need to type in the % explicitly for line\n", "magics; cell magics always require an explicit '%%' escape. By default,\n", "IPython ships with automagic on, so you should only rarely need the % escape.\n", "\n", "Example: typing '%cd mydir' (without the quotes) changes your working directory\n", "to 'mydir', if it exists.\n", "\n", "For a list of the available magic functions, use %lsmagic. For a description\n", "of any of them, type %magic_name?, e.g. '%cd?'.\n", "\n", "Currently the magic system has the following functions:\n", "%alias:\n", " No documentation\n", "%alias_magic:\n", " ::\n", " \n", " %alias_magic [-l] [-c] [-p PARAMS] name target\n", " \n", " positional arguments:\n", " name Name of the magic to be created.\n", " target Name of the existing line or cell magic.\n", " \n", " optional arguments:\n", " -l, --line Create a line magic alias.\n", " -c, --cell Create a cell magic alias.\n", " -p PARAMS, --params PARAMS\n", " Parameters passed to the magic function.\n", "%autoawait:\n", " No documentation\n", "%autocall:\n", " No documentation\n", "%automagic:\n", " No documentation\n", "%autosave:\n", " No documentation\n", "%bookmark:\n", " No documentation\n", "%cd:\n", " No documentation\n", "%clear:\n", " No documentation\n", "%cls:\n", " No documentation\n", "%colors:\n", " No documentation\n", "%conda:\n", " No documentation\n", "%config:\n", " No documentation\n", "%connect_info:\n", " No documentation\n", "%copy:\n", " Alias for `!copy`\n", "%ddir:\n", " Alias for `!dir /ad /on`\n", "%debug:\n", " ::\n", " \n", " %debug [--breakpoint FILE:LINE] [statement ...]\n", " \n", " positional arguments:\n", " statement Code to run in debugger. You can omit this in cell\n", " magic mode.\n", " \n", " optional arguments:\n", " --breakpoint , -b \n", " Set break point at LINE in FILE.\n", "%dhist:\n", " No documentation\n", "%dirs:\n", " No documentation\n", "%doctest_mode:\n", " No documentation\n", "%echo:\n", " Alias for `!echo`\n", "%ed:\n", " Alias for `%edit`.\n", "%edit:\n", " No documentation\n", "%env:\n", " No documentation\n", "%gui:\n", " No documentation\n", "%hist:\n", " Alias for `%history`.\n", "%history:\n", " ::\n", " \n", " %history [-n] [-o] [-p] [-t] [-f FILENAME] [-g [PATTERN ...]]\n", " [-l [LIMIT]] [-u]\n", " [range ...]\n", " \n", " positional arguments:\n", " range\n", " \n", " optional arguments:\n", " -n print line numbers for each input. This feature is only\n", " available if numbered prompts are in use.\n", " -o also print outputs for each input.\n", " -p print classic '>>>' python prompts before each input.\n", " This is useful for making documentation, and in\n", " conjunction with -o, for producing doctest-ready output.\n", " -t print the 'translated' history, as IPython understands\n", " it. IPython filters your input and converts it all into\n", " valid Python source before executing it (things like\n", " magics or aliases are turned into function calls, for\n", " example). With this option, you'll see the native\n", " history instead of the user-entered version: '%cd /'\n", " will be seen as 'get_ipython().run_line_magic(\"cd\",\n", " \"/\")' instead of '%cd /'.\n", " -f FILENAME FILENAME: instead of printing the output to the screen,\n", " redirect it to the given file. The file is always\n", " overwritten, though *when it can*, IPython asks for\n", " confirmation first. In particular, running the command\n", " 'history -f FILENAME' from the IPython Notebook\n", " interface will replace FILENAME even if it already\n", " exists *without* confirmation.\n", " -g <[PATTERN ...]> treat the arg as a glob pattern to search for in (full)\n", " history. This includes the saved history (almost all\n", " commands ever written). The pattern may contain '?' to\n", " match one unknown character and '*' to match any number\n", " of unknown characters. Use '%hist -g' to show full saved\n", " history (may be very long).\n", " -l <[LIMIT]> get the last n lines from all sessions. Specify n as a\n", " single arg, or the default is the last 10 lines.\n", " -u when searching history using `-g`, show only unique\n", " history.\n", "%killbgscripts:\n", " No documentation\n", "%ldir:\n", " Alias for `!dir /ad /on`\n", "%less:\n", " No documentation\n", "%load:\n", " No documentation\n", "%load_ext:\n", " No documentation\n", "%loadpy:\n", " No documentation\n", "%logoff:\n", " No documentation\n", "%logon:\n", " No documentation\n", "%logstart:\n", " No documentation\n", "%logstate:\n", " No documentation\n", "%logstop:\n", " No documentation\n", "%ls:\n", " Alias for `!dir /on`\n", "%lsmagic:\n", " No documentation\n", "%macro:\n", " No documentation\n", "%magic:\n", " No documentation\n", "%matplotlib:\n", " No documentation\n", "%mkdir:\n", " Alias for `!mkdir`\n", "%more:\n", " No documentation\n", "%notebook:\n", " ::\n", " \n", " %notebook filename\n", " \n", " positional arguments:\n", " filename Notebook name or filename\n", "%page:\n", " No documentation\n", "%pastebin:\n", " No documentation\n", "%pdb:\n", " No documentation\n", "%pdef:\n", " No documentation\n", "%pdoc:\n", " No documentation\n", "%pfile:\n", " No documentation\n", "%pinfo:\n", " No documentation\n", "%pinfo2:\n", " No documentation\n", "%pip:\n", " No documentation\n", "%popd:\n", " No documentation\n", "%pprint:\n", " No documentation\n", "%precision:\n", " No documentation\n", "%prun:\n", " No documentation\n", "%psearch:\n", " No documentation\n", "%psource:\n", " No documentation\n", "%pushd:\n", " No documentation\n", "%pwd:\n", " No documentation\n", "%pycat:\n", " No documentation\n", "%pylab:\n", " No documentation\n", "%qtconsole:\n", " No documentation\n", "%quickref:\n", " No documentation\n", "%recall:\n", " No documentation\n", "%rehashx:\n", " No documentation\n", "%reload_ext:\n", " No documentation\n", "%ren:\n", " Alias for `!ren`\n", "%rep:\n", " Alias for `%recall`.\n", "%rerun:\n", " No documentation\n", "%reset:\n", " No documentation\n", "%reset_selective:\n", " No documentation\n", "%rmdir:\n", " Alias for `!rmdir`\n", "%run:\n", " No documentation\n", "%save:\n", " No documentation\n", "%sc:\n", " No documentation\n", "%set_env:\n", " No documentation\n", "%store:\n", " No documentation\n", "%sx:\n", " No documentation\n", "%system:\n", " No documentation\n", "%tb:\n", " No documentation\n", "%time:\n", " No documentation\n", "%timeit:\n", " No documentation\n", "%unalias:\n", " No documentation\n", "%unload_ext:\n", " No documentation\n", "%who:\n", " No documentation\n", "%who_ls:\n", " No documentation\n", "%whos:\n", " No documentation\n", "%xdel:\n", " No documentation\n", "%xmode:\n", " No documentation\n", "%%!:\n", " No documentation\n", "%%HTML:\n", " Alias for `%%html`.\n", "%%SVG:\n", " Alias for `%%svg`.\n", "%%bash:\n", " %%bash script magic\n", " \n", " Run cells with bash in a subprocess.\n", " \n", " This is a shortcut for `%%script bash`\n", "%%capture:\n", " ::\n", " \n", " %capture [--no-stderr] [--no-stdout] [--no-display] [output]\n", " \n", " positional arguments:\n", " output The name of the variable in which to store output. This is a\n", " utils.io.CapturedIO object with stdout/err attributes for the\n", " text of the captured output. CapturedOutput also has a show()\n", " method for displaying the output, and __call__ as well, so you\n", " can use that to quickly display the output. If unspecified,\n", " captured output is discarded.\n", " \n", " optional arguments:\n", " --no-stderr Don't capture stderr.\n", " --no-stdout Don't capture stdout.\n", " --no-display Don't capture IPython's rich display.\n", "%%cmd:\n", " %%cmd script magic\n", " \n", " Run cells with cmd in a subprocess.\n", " \n", " This is a shortcut for `%%script cmd`\n", "%%debug:\n", " ::\n", " \n", " %debug [--breakpoint FILE:LINE] [statement ...]\n", " \n", " positional arguments:\n", " statement Code to run in debugger. You can omit this in cell\n", " magic mode.\n", " \n", " optional arguments:\n", " --breakpoint , -b \n", " Set break point at LINE in FILE.\n", "%%file:\n", " Alias for `%%writefile`.\n", "%%html:\n", " ::\n", " \n", " %html [--isolated]\n", " \n", " optional arguments:\n", " --isolated Annotate the cell as 'isolated'. Isolated cells are rendered\n", " inside their own