In [10]:
__AUTHORS__ = {'am': ("Andrea Marino",
"andrea.marino@unifi.it",),
'mn': ("Massimo Nocentini",
"massimo.nocentini@unifi.it",
"https://github.com/massimo-nocentini/",)}
__KEYWORDS__ = ['Python', 'Jupyter', 'notebooks', 'keynote',]
In [8]:
outline = []
outline.append('Hello!')
outline.append('Python')
outline.append('Whys and refs')
outline.append('On the shoulders of giants')
outline.append('Set the env up')
outline.append('Notebooks')
outline.append('Course agenda')
In [1]:
import this
From https://en.wikipedia.org/wiki/Python_(programming_language)
if, for, while and range — with some of its own twists, of course. Some supporting quotes here.
Python can be installed in many different ways with respect to different needs.
We advice to stick to the official one for the sake of being self contained and use an unified environment.
All such distributions customize the base package for domain-specific domains, in the future you will be able to take into account the one that best suites your needs; for the present, trust the default one.
Therefore, go head and install the Python interpreter.
There are many different possibilities to run Python programs:
my-script.py and then invoke the bare bone$ python my-script.pyWhatever you feel comfortable is okay.
The important thing is that you play in a safe environment.
Quoting the official doc:
Python provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.
Those environments allow you to freely (un)install modules and customize the interpreter, without apporting those changes to the system installation.
$ python3 -m venv unifi-env # creates a virtual environment
$ source unifi-env/bin/activate # enter into our safe environment
(unifi-env) $ pip install ipython jupyter matplotlib numpy scipy \
sympy pandas # install some packages
(unifi-env) $ python do-I-halt-or-not.py # run your cool stuff
(unifi-env) $ deactivate # exit the environment
$ # back to the usual shell
zeroqm messaging system
In [ ]:
In [36]:
%lsmagic
Out[36]:
For a nicer description evaluate the following cell:
In [12]:
%quickref
In [13]:
%%bash
jupyter-notebook -h
In [4]:
%%latex
\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{eqnarray}
%%timeitTime an example from your lab sessions (link):
In [5]:
initial_conditions = {0:0, 1:1}
def make_fibonacci(maxsize=None):
'''Make the Fibonacci sequence using memoization of not (set `maxsize` arg to 0)'''
@lru_cache(maxsize=maxsize)
def fibonacci(n):
return fibonacci(n-1) + fibonacci(n-2) if n not in initial_conditions else initial_conditions[n]
return fibonacci
In [6]:
%%timeit
fibonacci_memoization = make_fibonacci(maxsize=None)
[fibonacci_memoization(n) for n in range(20)]
In [7]:
%%timeit
fibonacci_naive = make_fibonacci(maxsize=0)
[fibonacci_naive(n) for n in range(20)]
In [16]:
import IPython.display
dir(IPython.display)[:10]
Out[16]:
In [49]:
from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
Out[49]:
In [50]:
from IPython.display import Latex
Latex(r"""\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{eqnarray}""")
Out[50]:
In [15]:
from IPython.display import HTML
oeis_url=r"http://oeis.org/"
HTML(r'<iframe width="100%" height="500" src="{url}" />'.format(url=oeis_url))
Out[15]:
nbviewerIf you publish your notebooks (Github, huh?), it is possible to render them as static web documents:
nbconvertIt is possible to convert a notebook to various formats:
In [53]:
%%bash
jupyter-nbconvert -h
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.