First and foremost, the IPython Notebook is an interactive environment for writing and running Python code.
Run a code cell using Shift-Enter or pressing the "Play" button in the toolbar above:
a = 10
print(a)
You can run a cell in place using Ctrl-Enter:
ls
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 "Stop" button in the toolbar above.
import time
time.sleep(10)
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 ctypes to segfault the Python interpreter:
import sys
from ctypes import CDLL
# This will crash a Linux or Mac system; equivalent calls can be made on Windows
dll = 'dylib' if sys.platform == 'darwin' else 'so.6'
libc = CDLL("libc.%s" % dll)
libc.time(-1) # BOOM!!
Define a variable in a Notebook, then use the "Kernel:Restart" menu item to restart the Kernel and verify that the variable has been cleared.
There are a number of ways of getting external code into code cells.
Pasting code with >>> prompts works as expected:
>>> the_world_is_flat = 1
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
The %load magic lets you load code from URLs or local files:
%load?
Go to the Matplotlib gallery, view the raw Python code for an example, use %load to load it into a cell and then run it. Make sure you run:
%pylab inline
first to enable plotting support.
%load soln/load.py
It is also possible to run all cells in a Notebook by using the "Cell" menu its "Run All".