Bienvenue ! Ceci est un notebook IPython.
1 + 3
IPython (Jupyter) est un système de présentation de documents scientifiques interactifs.
%%html
<p>Cette cellule <strong>contient du HTML</strong> !</p>
<p>Remarquez la <em>clef magique</em> <code>%%html</code>.</p>
Avec IPython sont aussi installés
...d'où Ju-Pyt-eR !
Le notebook est plus facile à utiliser si vous connaissez les raccourcis clavier. Echap
, puis h
pour les voir.
a = 1
a
type(a)
a = "toto"
a
type(a)
On ne combine pas deux types différents
a + 1
Nous n'allons pas entrer dans les détails des langages orientés aux objets. En pratique,
a.upper()
Dans la majorité des cas, les méthodes ne modifient pas l'objet, mais en créent un nouveau
b = a.upper()
b
a
Vous n'avez pas droit de laisser de l'espace blanc à gauche du code sans raison
a = 1
b = 2
Vous n'avez pas droit de couper une ligne
a =
2
a = "toto"
a == 'toto'
Il n'y a pas d'équivalent de char
a[1]
type(a[1])
Chaînes sur plusieurs lignes
a = "tot
o"
a = """
Mignonne, allons voir si la rose
Qui ce matin avoit desclose
Sa robe de pourpre au Soleil,
"""
a
Instruction print
pour afficher le texte
print a
Remplacement de valeurs (à la printf
)
"La reponse est %d" % 42
"La %s est %d" % ("question", 24)
a = 1
a == 1.0
Les nombres ont précision arbitraire (pas d'overflow comme en C)
Le L
à la fin indique un nombre long
a = 1000000
a * a * a * a * a * a
Opérateur puissance
a**6
True == (not False)
True and False
True or False
a = ["a", "b", "c"]
a[1]
len(a)
a[2] = "d"
a
a[3]
a[3] = "e"
a.append("e")
a
" ~~=> ".join(a)
a = { "couleur": "rouge", "forme": "rond"}
a
len(a)
a["couleur"]
a["taille"] = "grand"
a
if 1 + 1 == 2 and 1 - 1 > 0:
a = 10
print "faux"
elif 3 + 4 == 7:
a = 3
print "yes"
else:
a = 4
print "no"
a
while
¶i = 0
while i < 10:
print i
i += 1
Bloc for
(très différent du C)
a = ["a", "b", "c"]
for x in a:
print x
fonction range
pour obtenir une liste de nombres
range(10)
for i in range(10):
print i
def ma_fonction(x, y):
c = 2*x
return c + y
ma_fonction(2, 3)
Arguments par défaut
def fct(x, y=3, z=10):
return x + y + z
fct(2), fct(2, 4), fct(2, 4, 1), fct(2, z=3)
import numpy
from pylab import imshow, show
from timeit import default_timer as timer
Et maintenant commence la fête !
%pylab inline
def mandel(x, y, max_iters):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Mandelbrot
set given a fixed number of iterations.
"""
c = complex(x, y)
z = 0.0j
for i in range(max_iters):
z = z*z + c
if (z.real*z.real + z.imag*z.imag) >= 4:
return i
return max_iters
def create_fractal(min_x, max_x, min_y, max_y, image, iters):
height = image.shape[0]
width = image.shape[1]
pixel_size_x = (max_x - min_x) / width
pixel_size_y = (max_y - min_y) / height
for x in range(width):
real = min_x + x * pixel_size_x
for y in range(height):
imag = min_y + y * pixel_size_y
color = mandel(real, imag, iters)
image[y, x] = color
image = numpy.zeros((1024, 1536), dtype = numpy.uint8)
start = timer()
create_fractal(-2.0, 1.0, -1.0, 1.0, image, 20)
dt = timer() - start
print "Mandelbrot created in %f s" % dt
imshow(image)
show()