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.
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
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.$$
In [11]:
2+2
Out[11]:
In [6]:
2/3
Out[6]:
In [12]:
(1+2j)*(2+3j)
Out[12]:
Python knows about strings, and can manipulate them.
In [19]:
"This is a string"
Out[19]:
In [20]:
"This is a string" + "added to another string"
Out[20]:
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]:
In [23]:
doubleIt(2.0)
Out[23]:
In [25]:
doubleIt("Hi there ")
Out[25]:
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)
In [27]:
i = 0
while i<5:
i = i+1
print(i)
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
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]:
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]:
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]:
Check these out:
You can also use the notebook as a Beamer or Powerpoint replacement.
In [ ]: