A very attractive way to use python and work interactively is provided by jupyter notebooks
Great way to develop and document an interactive analysis and to share with others:
Starting:
In shell-window:
jupyter notebook
Sometimes problems on cip nodes, you might need:
jupyter notebook --browser=firefox
More info at https://jupyter.readthedocs.io/en/latest/index.html
<Enter>
and back with <Esc>
<Control>+<Enter>
: executes current cell<Shift>+<Enter>
: executes current cell and jumps to next cell;
)<m>
) or code type (<y>
)5**25
import numpy as np
import pylab as pl
t = np.arange(0.0, 1.0+0.01, 0.01)
s = np.cos(2*2*np.pi*t)
pl.plot(t, s)
pl.xlabel('time (s)')
pl.ylabel('voltage (mV)')
pl.title('About as simple as it gets, folks')
pl.grid(True)
Code cells
type(t)
print(t[0:10])
Notebooks
They can also be executed and stored on remote system and accessed via web-browser.
There are two modes in which jupyter notebooks are displayed:
To enter edit mode, press ENTER
. To leave it, press ESC
or CTRL+ENTER
(to execute code or render markdown) or SHIFT+ENTER
(to execute / render and move to the next cell).
There are mainly two different types of cells, code and markdown. To switch the type of a cell in navigation mode, press
y
for code type (for cells with code that is to be executed), orm
for markdown type (for adding documentation). You can also delete cells in navigation mode by pressing d
two times.
(Hint: You can find all keyboard shortcuts under "Help" of the notebook menu.)
Jupyter/IPython uses the ?
character as a shorthand for accessing documentation and other relevant information on any object, function or module:
In [2]: len?
Type: builtin_function_or_method
String form: <built-in function len>
Namespace: Python builtin
Docstring:
len(object) -> integer
Return the number of items of a sequence or mapping.
This notation works for just about anything, including object methods:
In [3]: L = [1, 2, 3]
In [4]: L.insert?
Type: builtin_function_or_method
String form: <built-in method insert of list object at 0x1024b8ea8>
Docstring: L.insert(index, object) -- insert object before index
or even objects themselves, with the documentation from their type:
In [5]: L?
Type: list
String form: [1, 2, 3]
Length: 3
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
Every Python object has various attributes and methods associated with it. This can be accessed in Python with help
or dir
.
But much more convenient is the tab-completion interface.
To see a list of all available attributes of an object, you can type the name of the object followed by a period (".
") character and the Tab key:
In [10]: L.<TAB>
L.append L.copy L.extend L.insert L.remove L.sort
L.clear L.count L.index L.pop L.reverse
IPython magic commands are prefixed by the %
character.
We just discuss two quick examples, but will see more in later chapters.
%run
¶Rather than writing or loading code in a cell it can be convenient to just run code in an external python file.
This can be done with the %run
magic.
In [6]: %run myscript.py
The output of the code will be shown and also all declarations (vars, functions, imports, ...) are available, it is equivalent to load and execute the code in a cell.
%timeit
¶This will determine the execution time of the single-line Python statement that follows it. For example, we may want to check the performance of a list comprehension:
In [8]: %timeit L = [n ** 2 for n in range(1000)]
1000 loops, best of 3: 325 µs per loop
The benefit of %timeit
is that for short commands it will automatically perform multiple runs in order to attain more robust results.
For multi line statements, adding a second %
sign will turn this into a cell magic that can handle multiple lines of input.
For example, here's the equivalent construction with a for
-loop:
In [9]: %%timeit
...: L = []
...: for n in range(1000):
...: L.append(n ** 2)
...:
1000 loops, best of 3: 373 µs per loop
Simple executon of shell commands, just precede with !
!pwd
a=!ls # ouput can be obtained as list
print (a)
We are using for the course the recent Python 3.7 version together with corresponding Numpy, Scipy, Pandas, etc, packages.
The installation is based on the Anaconda and conda tools, which provide scientific and machine learning python packages for many architectures (Linux, WIndows, Mac).
Pandas provides a function to get detailed info on installed package versions:
import pandas as pd
pd.show_versions()
For most parts differences between Python3 and Python2 are small:
print
is a function in Python3 and requires ()
(was statement in Python2)print ('Hi there') # Python3
print 'Hi there' # Python2
print ('Hi there')
, but not v.v.More info e.g. in http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html
We will use (only) Python 3. Python 2 was discontinued in 2020.