{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A quick tour of IPython Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tour will work a little better in interactive mode, so it'll be better if you get IPython notebook installed and running. You can start it from a terminal by running\n", "\n", "`ipython notebook`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we need to explain how to run cells. Try to run the cell below!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi! This is a cell. Press the ▶ button above to run it\n" ] } ], "source": [ "import pandas as pd\n", "\n", "print(\"Hi! This is a cell. Press the ▶ button above to run it\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also run a cell with Ctrl+Enter or Shift+Enter. Experiment a bit with that." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the most useful things about IPython notebook is its tab completion. \n", "\n", "Try this: click just after read_csv( in the cell below and press Shift+Tab (or Tab if you're using IPython 1.x) 4 times, slowly" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pd.read_csv(" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After the first time, you should see this:\n", "
\n", "\n", "
\n", "\n", "After the second time:\n", "
\n", "\n", "
\n", "\n", "After the fourth time, a big help box should pop up at the bottom of the screen, with the full documentation for the `read_csv` function:\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I find this amazingly useful. I think of this as \"the more confused I am, the more times I should press Shift+Tab\". Nothing bad will happen if you tab complete 12 times.\n", "\n", "Okay, let's try tab completion for function names!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pd.r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should see this:\n", "\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Writing code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Writing code in the notebook is pretty normal." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def print_10_nums():\n", " for i in range(10):\n", " print(i, end=',')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0,1,2,3,4,5,6,7,8,9," ] } ], "source": [ "print_10_nums()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Saving" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As of the latest stable version, the notebook autosaves. You should use the latest stable version. Really." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Magic functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IPython has all kinds of magic functions. Here's an example of comparing `sum()` with a list comprehension to a generator comprehension using the `%time` magic." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 7.93 ms, sys: 2.2 ms, total: 10.1 ms\n", "Wall time: 10.2 ms\n" ] }, { "data": { "text/plain": [ "4999950000" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time sum([x for x in range(100000)])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 8.49 ms, sys: 122 µs, total: 8.62 ms\n", "Wall time: 8.61 ms\n" ] }, { "data": { "text/plain": [ "4999950000" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time sum(x for x in range(100000))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The magics I use most are `%time` and `%prun` for profiling. You can run `%magic` to get a list of all of them, and `%quickref` for a reference sheet." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "%quickref" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also do nutty things like run Perl code in the notebook with cell magics. This is especially cool for things like Cython code, where you can try out Cython really fast with the `%%cython` magic (you'll need to install it)." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "whoa, perl!" ] } ], "source": [ "%%perl\n", "\n", "$_ = \"whoa, python!\";\n", "s/python/perl/;\n", "print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's it for now!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "