""" TP2 : Programmation Python — Boucles, Instructions de contrôle, Fonctions ---------------------------------------------------------------------------- Ce TP vous familiarise avec les structures fondamentales du langage Python : conditions, boucles et fonctions, à travers des exercices pratiques et un mini-projet. """ # --------------------------------------------------------------------- # PARTIE 1 : INSTRUCTIONS CONDITIONNELLES # --------------------------------------------------------------------- def pair_ou_impair(n): if n % 2 == 0: return f"{n} est pair." else: return f"{n} est impair." def appreciation_moyenne(note1, note2, note3): moyenne = (note1 + note2 + note3) / 3 if moyenne >= 16: appreciation = "Excellent" elif moyenne >= 12: appreciation = "Bien" elif moyenne >= 10: appreciation = "Passable" else: appreciation = "Insuffisant" return moyenne, appreciation # --------------------------------------------------------------------- # PARTIE 2 : BOUCLES # --------------------------------------------------------------------- def table_multiplication(n): table = [] for i in range(1, 11): table.append(f"{n} x {i} = {n * i}") return table def somme_de_1_a_n(N): somme = 0 for i in range(1, N + 1): somme += i return somme def boucle_while_somme(entrees): somme = 0 compteur = 0 for n in entrees: if n == 0: break somme += n compteur += 1 moyenne = somme / compteur if compteur > 0 else 0 return somme, moyenne # --------------------------------------------------------------------- # PARTIE 3 : FONCTIONS # --------------------------------------------------------------------- def factorielle(n): resultat = 1 for i in range(1, n + 1): resultat *= i return resultat def est_pair(n): return n % 2 == 0 def saluer(nom="inconnu"): return f"Bonjour {nom}, bienvenue dans le TP2 !" def operations(a, b): return a + b, a - b, a * b # --------------------------------------------------------------------- # PARTIE 4 : MINI-PROJET — CALCULATEUR STATISTIQUE # --------------------------------------------------------------------- def statistiques(notes): moyenne = sum(notes) / len(notes) var = sum((x - moyenne) ** 2 for x in notes) / len(notes) return moyenne, max(notes), min(notes), var # --------------------------------------------------------------------- # PROGRAMME PRINCIPAL DE DEMONSTRATION # --------------------------------------------------------------------- if __name__ == "__main__": print("=== TP2 : Programmation Python ===") print("\n-- Condition : Pair ou impair --") print(pair_ou_impair(7)) print(pair_ou_impair(10)) print("\n-- Appréciation des notes --") m, app = appreciation_moyenne(14, 10, 16) print(f"Moyenne = {m:.2f} | Appréciation = {app}") print("\n-- Table de multiplication (5) --") for ligne in table_multiplication(5): print(ligne) print("\n-- Somme de 1 à 10 --") print("Somme =", somme_de_1_a_n(10)) print("\n-- Boucle while simulée --") somme, moy = boucle_while_somme([5, 3, 2, 0]) print(f"Somme = {somme}, Moyenne = {moy}") print("\n-- Factorielle de 5 --") print("5! =", factorielle(5)) print("\n-- Vérification de parité --") print(est_pair(8), est_pair(9)) print("\n-- Salutations --") print(saluer("Alice")) print("\n-- Opérations --") s, d, p = operations(10, 5) print(f"Somme={s}, Différence={d}, Produit={p}") print("\n-- Statistiques sur les notes [10, 15, 18, 12] --") m, maxv, minv, v = statistiques([10, 15, 18, 12]) print(f"Moyenne={m:.2f}, Max={maxv}, Min={minv}, Variance={v:.2f}")