In [1]:
def pair( n ):
return n == 0 or impair( n - 1 )
def impair( n ):
return n != 0 and pair( n - 1 )
In [2]:
pair(22)
Out[2]:
In [3]:
def pair2( n ):
n=abs(n)
return n == 0 or ( ( n - 1 ) != 0 and pair2( n - 2 ) )
In [4]:
def impair2( n ):
n=abs(n)
return n != 0 and ( ( n - 1 ) == 0 or impair2( n - 2 ) )
In [5]:
def pascal(n):
if n == 1:
return [1]
else:
ligne = [1]
ligne_precedente = pascal(n-1)
for it in range(len(ligne_precedente)-1):
ligne.append(ligne_precedente[it] + ligne_precedente[it+1])
ligne += [1]
return ligne
In [6]:
print(pascal(6))
In [7]:
def chaine_de_pascal(n):
s=''
for it in range(len(pascal(n))):
s1=str(pascal(n)[it])
s = s + '-' + s1
return s + '-'
for it in range(1,10):
print(chaine_de_pascal(it).center(35))
In [8]:
def arbre_de_pascal(n):
for it in range(1,n+1):
print(chaine_de_pascal(it).center(35))
In [9]:
arbre_de_pascal(5)
In [10]:
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
In [11]:
from IPython.display import display
text = widgets.Text()
display(text)
def handle_submit(sender):
print(print(chaine_de_pascal(int(text.value))))
text.on_submit(handle_submit)
In [12]:
interact(arbre_de_pascal, n=(1,11,1))
In [13]:
button = widgets.Button(description="Appuyez Ici !")
display(button)
def on_button_clicked(b):
print("La valeur de la chaine de Pascal jusqu'à {}".format(text.value))
print("est.....")
for it in range(1,int(text.value)+1):
print(chaine_de_pascal(it).center(35))
print("voila.....")
button.on_click(on_button_clicked)
In [14]:
%matplotlib inline
import matplotlib.pyplot as plt
#from IPython.html.widgets import *
from ipywidgets import *
import numpy as np
def plot_sinus(f):
"""
Usage:
Donne le graphe d'une fonction sinus définie sur un intervale d'une période
Entrées:
- f est un parametre formel correspondant à la fréquence
Liens:
Est appelée par la fonction graphique interact qui passe f
"""
x=np.arange(0.0,1.0,0.01)
plt.plot(x,np.sin(2*np.pi*x*f))
plt.show()
#Declaration de la fréquence
#f=5
#plot_sinus(f)
# On peux effectivement passer une série de parametres grace à la fonction interact
interact(plot_sinus, f=(1,10,0.1))
Out[14]:
In [15]:
def f(passionant):
print("Le cours d'info est passionant, isn't is ?" + '\n'
+"That is indeed {}...".format(passionant))
interact(f, passionant=True)
Out[15]: