On the rest of the course, we will use the jupyter notebook
s to do our codes.
During this class we will learn how to use the notebook, install packages (We will need this further), and importing modules constructed by ourselves.
pip
¶While programming, we will need to have libraries/packages, these Libraries are code written by someone else that we can use without program it.
The construction of these libraries/packages is done by writing functions so that we do not care about how the implementation is exactly done, but by the input and output, that is why you have to read the documentation!!.
To use the libraries/packages, they must be installed, and there are several ways to do this.
For example, let us suppose that we don't have python3
on our computers, to install it we use
- on
Ubuntu
sudo apt-get install python3Or, on the newest version
sudo apt install python3
Once we have python3
we would like to have some packages to use them on our codes, for example numpy
offers a very useful data type implemented numpy.array
, and if we want to use them, we would have to install it.
The simplest way to do this is,
- on
Ubuntu
sudo apt install python3-numpy- on
MacOS
brew install python3-numpy
It is really easy, but sometimes different versions of libraries have implemented new routines, and the task of compatibility and update among them can get complicated.
To solve this, pip
was created. pip
is a python
package installer. doc
First of all, we have to install it,
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.pyand then run
python3 get-pip.py
The installing documentation can be found here link
Then we install our packages just by using
pip3 install numpy
During the course, we are also going to use scipy
and matplotlib
.
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. link
The recommended installaton is link
python3 -m pip install --upgrade pip
python3 -m pip install jupyter
Once installed, type on the terminal prompt
jupyter notebook
Here you can see the options yuo have on the notebook
Here you can find,
To execute a cell (markdown or code), Shit+enter
.
On the code cells, you will see one small number In[1]
That number tells you the order of execution of the cells.
Note You can erase an already runned cell, but their effects will remain!
If you see In[*]
means that is running or waiting.
One can construct the libraries/package ourself, these are called Modules
, let us construct a file with some functions on it, for example
%%bash
more prog.py
def print_message(string): print(string) def sum_nums(a,b): return a+b def fac(n): if n==0 or n==1: return 1 else: return n*fac(n-1)
There you can see how to use magic cells on the notebook. I used a bash line to print a file on the notebook!
The file prog.py
have implemented some functions on it, the idea is that we want to use them on a different file.
first, if we have the code on the same folder, we just import the functions by using
import prog
We use prog
because is the name of the file without extension.
if we do not know what the functions names are, we can use the function dir
dir(prog)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fac', 'print_message', 'sum_nums']
So, now we know that the functions fac
, print_message
, sum_nums
are implemented, so let us use them.
the basic syntax is library.functions(parameters)
, for instance
prog.print_message('test')
test
prog.sum_nums(1,2)
3
prog.fac(6)
720
You can also use a nickname for the library while programming
import prog as p
p.fac(3)
6
p.print_message('Test 2')
Test 2
This is also used but NOT RECOMMENDED!, because there are some libreries with different functions but with the same name.
from prog import *
print_message('Test 3')
Test 3
fac(4)
24
fac(5)
120
You can also import a single function
from prog import fac
fac(10)
3628800