Introducción a python científico

python es un lenguaje genérico, así que, casi siempre, hay que importar módulos, o bien enteros:


In [3]:
import scipy as np

o bien submódulos:


In [4]:
from matplotlib import pyplot as plt

Para ver los gráficos dentro de la hoja (% es un comando "mágico")


In [5]:
%matplotlib inline

Vectores numéricos


In [6]:
x=np.linspace(1,10,100)

In [7]:
x


Out[7]:
array([  1.        ,   1.09090909,   1.18181818,   1.27272727,
         1.36363636,   1.45454545,   1.54545455,   1.63636364,
         1.72727273,   1.81818182,   1.90909091,   2.        ,
         2.09090909,   2.18181818,   2.27272727,   2.36363636,
         2.45454545,   2.54545455,   2.63636364,   2.72727273,
         2.81818182,   2.90909091,   3.        ,   3.09090909,
         3.18181818,   3.27272727,   3.36363636,   3.45454545,
         3.54545455,   3.63636364,   3.72727273,   3.81818182,
         3.90909091,   4.        ,   4.09090909,   4.18181818,
         4.27272727,   4.36363636,   4.45454545,   4.54545455,
         4.63636364,   4.72727273,   4.81818182,   4.90909091,
         5.        ,   5.09090909,   5.18181818,   5.27272727,
         5.36363636,   5.45454545,   5.54545455,   5.63636364,
         5.72727273,   5.81818182,   5.90909091,   6.        ,
         6.09090909,   6.18181818,   6.27272727,   6.36363636,
         6.45454545,   6.54545455,   6.63636364,   6.72727273,
         6.81818182,   6.90909091,   7.        ,   7.09090909,
         7.18181818,   7.27272727,   7.36363636,   7.45454545,
         7.54545455,   7.63636364,   7.72727273,   7.81818182,
         7.90909091,   8.        ,   8.09090909,   8.18181818,
         8.27272727,   8.36363636,   8.45454545,   8.54545455,
         8.63636364,   8.72727273,   8.81818182,   8.90909091,
         9.        ,   9.09090909,   9.18181818,   9.27272727,
         9.36363636,   9.45454545,   9.54545455,   9.63636364,
         9.72727273,   9.81818182,   9.90909091,  10.        ])

In [8]:
plt.plot(x)


Out[8]:
[<matplotlib.lines.Line2D at 0x7fb463823190>]

Funciones de vectores


In [9]:
y=np.cos(x)

In [11]:
plt.plot(x , y )


Out[11]:
[<matplotlib.lines.Line2D at 0x7fb463735950>]

Índices


In [12]:
x[0]


Out[12]:
1.0

In [13]:
y[0]


Out[13]:
0.54030230586813977

Probar: x.[TAB]


In [17]:
x.size


Out[17]:
100

In [19]:
x[100]   # OJO!!!!


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-19-5d9d972c73ab> in <module>()
----> 1 x[100]   # OJO!!!!

IndexError: index 100 is out of bounds for axis 0 with size 100

In [20]:
x[99]


Out[20]:
10.0

In [21]:
x[0:2]


Out[21]:
array([ 1.        ,  1.09090909])

Estructuras de control

¡¡ Atención al sangrado !!


In [24]:
for xx in x:
    print xx


1.0
1.09090909091
1.18181818182
1.27272727273
1.36363636364
1.45454545455
1.54545454545
1.63636363636
1.72727272727
1.81818181818
1.90909090909
2.0
2.09090909091
2.18181818182
2.27272727273
2.36363636364
2.45454545455
2.54545454545
2.63636363636
2.72727272727
2.81818181818
2.90909090909
3.0
3.09090909091
3.18181818182
3.27272727273
3.36363636364
3.45454545455
3.54545454545
3.63636363636
3.72727272727
3.81818181818
3.90909090909
4.0
4.09090909091
4.18181818182
4.27272727273
4.36363636364
4.45454545455
4.54545454545
4.63636363636
4.72727272727
4.81818181818
4.90909090909
5.0
5.09090909091
5.18181818182
5.27272727273
5.36363636364
5.45454545455
5.54545454545
5.63636363636
5.72727272727
5.81818181818
5.90909090909
6.0
6.09090909091
6.18181818182
6.27272727273
6.36363636364
6.45454545455
6.54545454545
6.63636363636
6.72727272727
6.81818181818
6.90909090909
7.0
7.09090909091
7.18181818182
7.27272727273
7.36363636364
7.45454545455
7.54545454545
7.63636363636
7.72727272727
7.81818181818
7.90909090909
8.0
8.09090909091
8.18181818182
8.27272727273
8.36363636364
8.45454545455
8.54545454545
8.63636363636
8.72727272727
8.81818181818
8.90909090909
9.0
9.09090909091
9.18181818182
9.27272727273
9.36363636364
9.45454545455
9.54545454545
9.63636363636
9.72727272727
9.81818181818
9.90909090909
10.0

In [30]:
if x[0] < 1.2 :
    print "Menor que 1.2"


Menor que 1.2

In [32]:
for xx in x:
    if xx < 1.2 :
        print "Menor que 1.2"


Menor que 1.2
Menor que 1.2
Menor que 1.2