Using Python in Jupyter

This is a typical notebook in Jupyter.

It is organized as a series of cells. Each cell could contain text, code, or some raw format (Raw NBConvert).

You can select which type of code you want to run. For this notebook, we are using Python 3.

You could also choose to use Python 2, Julia, or R. Additional languages can be installed.

Running your cells

You edit a cell by clicking on it and typing as you wish.

To run it (either code, or to format text), you must hit "Shift-Enter" on your keyboard.

Using Latex and such

The text is formatted using something called the Markdown language. This language is useful for simple formatting like making lists, adding bold face or italics, and using basic Latex syntax.

A list is formed using dashes at the start of each line. Hit "Shift-return" to get the formatted version

  • list item one
  • list item two
  • and so on.

Italics are formed with single asterix, bold with double. Like this and like this.

Latex works fine, except you don't have access to fancy packages. Use single dollar signs for inline, double for display.

This is an integral formula inline $\sin(x) = \int_0^x \cos(t) dt$, while here it is again in display mode: $$\sin(x) = \int_0^x \cos(t) dt.$$

Creating code

This is as simple as typing some legal Python code into a cell, and hitting "Shift-Enter" to run it. Or use the menu commands.

Python knows about integers, floating point numbers, even complex numbers like $1+2j$:


In [11]:
2+2


Out[11]:
4

In [6]:
2/3


Out[6]:
0.6666666666666666

In [12]:
(1+2j)*(2+3j)


Out[12]:
(-4+7j)

Python knows about strings, and can manipulate them.


In [19]:
"This is a string"


Out[19]:
'This is a string'

In [20]:
"This is a string" + "added to another string"


Out[20]:
'This is a stringadded to another string'

You can define functions in Python. Notice that it is not fussy about declaring the type of the arguments. (Unlike C or Fortran.)


In [21]:
def doubleIt(x):
    return x+x

In [22]:
doubleIt(2)


Out[22]:
4

In [23]:
doubleIt(2.0)


Out[23]:
4.0

In [25]:
doubleIt("Hi there ")


Out[25]:
'Hi there Hi there '

You can define for loops, and while loops, using a simple syntax. Notice the colon, the indenting, and the fact the loop definition ends with a blank line. That's all part of the syntax.


In [26]:
for i in {1,2,3}:
    print(i)


1
2
3

In [27]:
i = 0 
while i<5:
    i = i+1
    print(i)


1
2
3
4
5

You can run python code that you saved on disk as a file. For instance, in this folder we have a text file called manysring.py . The code looks like this

s = 'x'
while len(s)<10:
    print(s)
    s = s+'x'

To run it, use the magic command called %run


In [34]:
%run manystring


x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx
xxxxxxxxx

If you want to do something more complicated, you will have to load in various packages and functions within them.

For instance, to compute a sine function, you need to use Numerical Python, also known as Numpy.


In [36]:
from numpy import sin
sin(.1)


Out[36]:
0.099833416646828155

You will find it to import everything in numpy, then you have access to lots of useful functions, arrays, etc.


In [38]:
from numpy import *
[sin(.1),cos(.1),tan(.1),arcsin(.1),arccos(.1),arctan(.1)]


Out[38]:
[0.099833416646828155,
 0.99500416527802571,
 0.10033467208545055,
 0.1001674211615598,
 1.4706289056333368,
 0.099668652491162038]

Namespaces

Actually, for big projects, you should not use the import-star command, as the many function names may clash with other function definitions in your code, or other packages.

It is considered safer to import the functions with the name of the package attached to them, or an appropriate abbreviation. Like this:


In [41]:
import numpy as np
[np.sin(.1),np.cos(.1),np.tan(.1),np.arcsin(.1),np.arccos(.1),np.arctan(.1)]


Out[41]:
[0.099833416646828155,
 0.99500416527802571,
 0.10033467208545055,
 0.1001674211615598,
 1.4706289056333368,
 0.099668652491162038]

Wrap up.

At this point, you now know how to access Python through the Jupyter notebook. To do some serious coding, you need to learn more Python. This workshop is not specifically about Python, so we will stop here.

Other things to explore in the Jupyter Notebook.

Check these out:

  • File menu, for saving and downloading copies of your notebook.
  • Edit menu allows you to cut, copy, paste entires cells and groups of cells.
  • View menu, to decrease the clutter on your screen.
  • Insert menu, to add cells to a notebook where you need them.
  • Cell menu, lets you run a bunch of cells at once.
  • Kernel menu, to restart when necessary, or even change programming languages.
  • Help menu, yay!

You can also use the notebook as a Beamer or Powerpoint replacement.


In [ ]: