Prise en main du langage Python avec Pyzo afin de maîtriser rapidement le langage de base à l’arrivée en sup. Je t’accompagne dans tes premiers pas ! On aborde la prise en main du logiciel, la rédaction du code, les types (entiers, flottants, booléens, chaines de caractères, listes). On manipule des listes, révise le range, on crée des boucles for et while et on utilise la conditon if.
VIDEO
Sujet PDF du TP :
proxy.php?id=pdf-itc1-0-1
Programmer ce TP directement ici sur Basthon :
↑ Ce Basthon reste en cache du navigateur ↑
Visualiser le Basthon correction
Visualiser les liens Dropbox vers les différents documents de ce TD
Codes non exécutables indépants de Basthon et Dropbox (peuvent ne pas être à jour)
''' Auteur: Denis DEFAUCH Y
Contenu sous licence , consulter www . cpge - sii . com '''
''' 0 - Bases Python '''
## 0-1 - Prise en mai n
# Question 1
x = 42
y = 42.0
type ( x )
type ( y )
x = x + y
type ( x )
z = ( 1 > 0 )
z
type ( z )
t = ( 0 == 1 ) or ( 1 != 2 )
t
L = [ 1 , 3 , 6 , 9 , 12 ]
type ( L )
L [ 0 ]
L [ 1 ]
len ( L )
L [ 4 ]
# L [ 5 ]
L [ - 1 ]
L [ - 2 ]
L [ 1 : 4 ]
L [ 4 : 1 ]
L + L
L . append ( 15 )
L
dir ( L )
help ( L . append )
L = L . append ( 18 )
L
range ( 2 , 5 )
list ( range ( 2 , 5 ))
list ( range ( 5 , 2 ))
list ( range ( - 5 , - 2 ))
list ( range ( - 2 , - 5 ))
# Question 2
def somme1 ( n ):
""" Renvoie 0+1+2+...+ n
Précondition : n doit être un entier naturel """
s = 0
for k in range ( n + 1 ):
s += k
return s
test = somme1 ( 5 )
print ( test )
# Question 3
somme1 ( 10 )
somme1 ( - 2 )
somme1 ( 120 ) == 120 * 121 / 2
help ( somme1 )
# Question 4
def factorielle1 ( n ):
res = 1
for i in range ( 1 , n + 1 ):
res *= i
return res
factorielle1 ( 0 )
factorielle1 ( 1 )
factorielle1 ( 5 )
# Question 5
def factorielle2 ( n ):
res = 1
k = n
while k > 1 :
res *= k
k -= 1
return res
factorielle2 ( 0 )
factorielle2 ( 1 )
factorielle2 ( 5 )
# Question 6
L1 = [ i for i in range ( 10 )]
L2 = [ compteur ** 2 for compteur in range ( 7 )]
L3 = [ j + 1 for j in range ( - 2 , 5 )]
L4 = [ j for j in range ( 1 , 8 , 2 )]
L5 = []
for k in range ( 5 ):
L5 . append ( k ** 2 )
dir ( L1 )
# Question 7
def generer_liste_entiers_1 ( deb , fin ):
L = []
for i in range ( deb , fin + 1 ):
L . append ( i )
return L
generer_liste_entiers_1 ( 10 , 15 )
# Question 8
def generer_liste_entiers_2 ( deb , fin ):
L = []
i = deb
while i <= fin :
L . append ( i )
i += 1
return L
generer_liste_entiers_2 ( 10 , 15 )
# Question 9
def liste_diviseurs ( n ):
L = []
for i in range ( 1 , n ):
if n % i == 0 : # n divisible par i
L . append ( i )
return L
liste_diviseurs ( 50 )
# Question 10
def somme2 ( L ):
somme = 0
for i in range ( len ( L )):
somme += L [ i ]
return somme
somme2 ([ 1 , 2 , 3 , 4 , 5 ])
# Question 11
def somme3 ( L ):
somme = 0
for t in L :
somme += t
return somme
somme3 ([ 1 , 2 , 3 , 4 , 5 ])
# Question 12
def est_parfait ( n ):
Div = liste_diviseurs ( n )
S = somme2 ( Div )
return S == n
est_parfait ( 6 )
est_parfait ( 8 )
# Question 13
def liste_parfait_1 ( N ):
Res = []
for n in range ( 1 , N ):
if est_parfait ( n ):
Res . append ( n )
return Res
liste_parfait_1 ( 1000 )
# Question 14
def liste_parfait_2 ( N ):
Res = []
n = 1
while len ( Res ) < N :
if est_parfait ( n ):
Res . append ( n )
n += 1
return Res
liste_parfait_2 ( 3 )
# Question 15
LP = liste_parfait_2 ( 4 )
quatrieme_nombre = LP [ - 1 ]