Ipython vs Python

All valid Python syntax works as-is in IPython. Ipython also provides a number of extra features.

Running code and getting help

In the notebook, to run a cell of code, hit Shift-Enter. This executes the cell and puts the cursor in the next cell below, or makes a new one if you are at the end. Alternately, you can use:

  • Alt-Enter to force the creation of a new cell unconditionally.
  • Control-Enter executes the cell and keeps the cursor in the same cell.

Running code:

In [1]:
print("Hello world!")
Hello world!

Getting help:

In [2]:
import distutils
In [3]:
In [4]:

Pressing Shift-Tab is another way of getting help

In [ ]:

Tab completion:

In [ ]:
In [5]:
this_is_a_variable = 42
this_is_a_function = lambda x: x
In [ ]:

Accessing the underlying operating system

You can use shell commands by placing a ! in front of the commands

In [6]:

The results of shell commands can be assigned to python variables

In [7]:
dir_list = !ls
In [8]:
print("The content of the directory is:\n\n{}".format(dir_list))
The content of the directory is:

['analysis.html', 'analysis_slideshow.slides.html', 'display.ipynb', 'enviroment.ipynb', 'example', 'example_file.txt', 'example_script.py', 'export.ipynb', 'figs', 'index.ipynb', 'installation.ipynb', 'magics.ipynb', 'output_toggle.tpl', 'parallel.ipynb', 'references.ipynb', 'use_of_ipython.ipynb']

Python variables (and expressions) can then be used in shell commands

In [9]:
!echo "The content of the directory is:\n"
!echo $dir_list
The content of the directory is:

[analysis.html, analysis_slideshow.slides.html, display.ipynb, enviroment.ipynb, example, example_file.txt, example_script.py, export.ipynb, figs, index.ipynb, installation.ipynb, magics.ipynb, output_toggle.tpl, parallel.ipynb, references.ipynb, use_of_ipython.ipynb]
In [10]:
!echo {[item for item in dir_list if item.endswith(".ipynb")]}
[display.ipynb, enviroment.ipynb, export.ipynb, index.ipynb, installation.ipynb, magics.ipynb, parallel.ipynb, references.ipynb, use_of_ipython.ipynb]