Python 2023 2024
Python 2023 2024
▪ Lecture:
▪ Syntaxe:
input()
▪ Exemple:
X=float(input(donner un nombre réel: ))
▪ Ecriture:
▪ Syntaxe:
print(message )
Indentation
▪ Python utilise l’indentation du code avec des caractères blancs
plutôt que des mots clés (debut / fin)
Choix
▪ Syntaxe:
if conditions :
blocs d’instructions
▪ Exemple:
a = -150
if a <0:
print ('a est négatif’)
Choix
▪ Choix double:
▪ Syntaxe:
if conditions :
blocs d’instructions 1
else:
blocs d’instructions 2
▪ Exemple:
if a <0:
print ('a est négatif’)
else:
print('a est positif ’)
Choix
▪ Choix Multiple:
▪ Syntaxe:
if conditions1 :
blocs d’instructions 1
elif condiotions2 :
blocs d’instructions 2
elif conditions I :
...
else:
autres instructions
choix
▪ Exemple de Choix Multiple:
a = 10.
if a > 0:
print( 'a est strictement positif’ )
if a >= 10:
print( 'a est un nombre’)
else:
print( 'a est un chiffre’)
a += 1
elif a is not 0:
print( 'a est strictement négatif’)
else:
print( 'a est nul’ )
boucles
▪ While:
▪ syntaxe:
while conditions:
instructions
▪ Instructions particuliers:
▪ Syntaxe:
for cible in séquence d’objets:
bloc instructions
▪ range:
▪ Syntaxe
range(start,stop+1,step)
▪ Exemple:
range(6) donne la séquence(liste) [0,1,2,3,4,5]
range(1,6)----->[1,2,3,4,5]
range(1,6,3)----->[1,4]
Boucles
▪ For: Exemple:
prod = 1
for p in range(1, 10):
prod *= p
print(prod)
▪ Exécution:
1
2
6
24
120
720
5040
40320
362880