{
"metadata": {
"name": "",
"signature": "sha256:1cf51b66a39fb370f2fb3d08af95cfc79b8884f310509181b3b0586400e20b81"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Running Code in the IPython Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First and foremost, the IPython Notebook is an interactive environment for writing and running code. IPython is capable of running code in a wide range of languages. However, in IPython 2.0, the default kernel runs Python code."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Code cells allow you to enter and run Python code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run a code cell using `Shift-Enter` or pressing the button in the toolbar above:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 10"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are two other keyboard shortcuts for running code:\n",
"\n",
"* `Alt-Enter` runs the current cell and inserts a new one below.\n",
"* `Ctrl-Enter` run the current cell and enters command mode."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Managing the IPython Kernel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Code is run in a separate process called the IPython Kernel. The Kernel can be interrupted or restarted. Try running the following cell and then hit the button in the toolbar above."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time\n",
"time.sleep(10)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the Kernel dies you will be prompted to restart it. Here we call the low-level system libc.time routine with the wrong argument via\n",
"ctypes to segfault the Python interpreter:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import sys\n",
"from ctypes import CDLL\n",
"# This will crash a Linux or Mac system\n",
"# equivalent calls can be made on Windows\n",
"dll = 'dylib' if sys.platform == 'darwin' else 'so.6'\n",
"libc = CDLL(\"libc.%s\" % dll) \n",
"libc.time(-1) # BOOM!!"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Cell menu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The \"Cell\" menu has a number of menu items for running code in different ways. These includes:\n",
"\n",
"* Run and Select Below\n",
"* Run and Insert Below\n",
"* Run All\n",
"* Run All Above\n",
"* Run All Below"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Restarting the kernels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The kernel maintains the state of a notebook's computations. You can reset this state by restarting the kernel. This is done by clicking on the in the toolbar above."
]
}
],
"metadata": {}
}
]
}