← Retour
1. Les bases de Python
Variables :
x = 5 # entier
nom = "Alice" # chaine
prix = 3.14 # décimal
ok = True # booléen
2. Entrées / Sorties
nom = input("Votre nom : ")
print("Bonjour", nom)
3. Conditions
note = float(input("Note : "))
if note >= 10:
print("Admis")
elif note >= 8:
print("Rattrapage")
else:
print("Refusé")
4. Boucle for
for i in range(5): # 0,1,2,3,4
print(i)
for i in range(1, 11): # 1 à 10
print(i * 3)
5. Boucle while
n = 1
while n <= 5:
print(n)
n = n + 1
6. Exemples métiers
Calcul de moyenne :
total = 0
n = int(input("Nb de valeurs : "))
for i in range(n):
v = float(input(f"Valeur {i+1} : "))
total += v
print("Moyenne :", total/n)
Taux de défaut :
total = int(input("Pièces produites : "))
defaut = int(input("Pièces défectueuses : "))
taux = defaut / total * 100
print(f"Taux de défaut : {taux:.2f}%")