None IPythonNotebookIntroduction

Running Code in the IPython Notebook

Based on this notebook with very minor changes.


First and foremost, the IPython Notebook is an interactive environment for writing and running Python code.

Code cells allow you to enter and run Python code

Run a code cell using Shift-Enter or pressing the "Play" button in the toolbar above:

In [1]:
a = 10
In [2]:
print(a)
10

All of the goodness of IPython works

Here are two system aliases:

In [3]:
pwd
Out[3]:
'/Users/py4cs/Desktop/code/notebooks'
In [9]:
ls
IPython-beyond-plain-Python-Fernando-Perez.html
IPython-beyond-plain-Python-Fernando-Perez.ipynb
IPythonNotebookIntroduction.html
IPythonNotebookIntroduction.ipynb
Matplotlib.html
Matplotlib.ipynb
Testing-intro.html
Testing-intro.ipynb
Untitled.ipynb
Untitled1.ipynb
Untitled2.ipynb
Untitled3.ipynb
Untitled4.ipynb
Untitled5.ipynb
filename.pdf
filename.png
info.txt
pandas-intro.html
pandas-intro.ipynb
test.svg

Any command line program can be run using !

In [12]:
!uptime
12:58  up 4 days, 18:26, 9 users, load averages: 3.44 4.03 4.43

For ! we can use string interpolation from Python variables:

In [5]:
message = 'The IPython notebook is great!'
# note: the echo command does not run on Windows, it's a unix command.
!echo $message
The IPython notebook is great!

Tab completion works:

In [1]:
import numpy
In [ ]:
numpy.

Shift-Tab completion after ( brings up a tooltip with the docstring:

In [ ]:
numpy.random.rand(

Adding ? opens the docstring in the pager below:

In [23]:
magic?

Exceptions are formatted nicely:

In [2]:
x = 1
y = 4
z = y/(1-x)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-2-d93e730d8440> in <module>
      1 x = 1
      2 y = 4
----> 3 z = y/(1-x)

ZeroDivisionError: division by zero

Working with external code

There are a number of ways of getting external code into code cells.

Pasting code with >>> prompts works as expected:

In [7]:
>>> the_world_is_flat = True
>>> if the_world_is_flat:
...     print("Be careful not to fall off!")
Be careful not to fall off!

The %load magic lets you load code from URLs or local files:

In [3]:
%load?
In [13]:
%matplotlib inline
%config InlineBackend.figure_formats = ['svg']
In [15]:
%load https://matplotlib.org/stable/_downloads/e08194fbd26985341907d3fbfbe07ddb/histogram_features.py
2021-04-21T12:59:24.354060 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
Out[15]:
<function matplotlib.axes._axes.Axes.set_ylabel(self, ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs)>

Creating a file on the fly

Using the %%file magic, we can directly create a file

In [16]:
%%file helloworld.py
print("Hello World")
Writing helloworld.py

and can see the file on the file system:

In [17]:
!ls -l helloworld.py
-rw-r--r--  1 py4cs  staff  21 Apr 21 12:59 helloworld.py

and execute the file using the python interpreter:

In [18]:
!python helloworld.py
Hello World

Type markdown for text and latex

For example, if $x=\alpha$ then $x^2 = \alpha^2$. Furthermore $$ \int\limits_a^b f(x) \mathrm{d} x = \Gamma.$$

Further reading

In [ ]: