{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Running Code"
]
},
{
"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, this notebook, and the default kernel in IPython 2.0, runs Python code."
]
},
{
"cell_type": "markdown",
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(a)"
]
},
{
"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": "markdown",
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import time\n",
"time.sleep(10)"
]
},
{
"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",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"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!!"
]
},
{
"cell_type": "markdown",
"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": "markdown",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sys.stdout and sys.stderr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The stdout and stderr streams are displayed as text in the output area."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"hi, stdout\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"print('hi, stderr', file=sys.stderr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Output is asynchronous"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, you will see the output one piece at a time, not all at the end."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import time, sys\n",
"for i in range(8):\n",
" print(i)\n",
" time.sleep(0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Large outputs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To better handle large outputs, the output area can be collapsed. Run the following cell and then single- or double- click on the active area to the left of the output:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in range(50):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Beyond a certain point, output will scroll automatically:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for i in range(500):\n",
" print(2**i - 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}