Travaux Pratiques Python + Solutions
TP1.1 – Somme et produit
Énoncé : Écrire un programme qui demande à l’utilisateur deux nombres et affiche leur somme et
leur produit.
Solution :
a = int(input("Entrez un premier nombre : "))
b = int(input("Entrez un deuxième nombre : "))
print("Somme :", a + b)
print("Produit :", a * b)
TP1.2 – Pair ou impair
Énoncé : Demander à l’utilisateur un nombre et indiquer s’il est pair ou impair.
Solution :
n = int(input("Entrez un nombre : "))
if n % 2 == 0:
print("Le nombre est pair")
else:
print("Le nombre est impair")
TP1.3 – Table de multiplication
Énoncé : Afficher la table de multiplication d’un nombre choisi par l’utilisateur.
Solution :
n = int(input("Entrez un nombre : "))
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
TP1.4 – Fonction factorielle
Énoncé : Écrire une fonction factorielle(n) qui calcule la factorielle d’un entier.
Solution :
def factorielle(n):
resultat = 1
for i in range(1, n+1):
resultat *= i
return resultat
print("5! =", factorielle(5))
TP2.1 – Analyse d’un texte
Énoncé : Lire un fichier texte et afficher nombre de lignes, de mots et de caractères.
Solution :
def analyse_fichier(nom_fichier):
with open(nom_fichier, "r", encoding="utf-8") as f:
texte = [Link]()
lignes = [Link]()
mots = [Link]()
print("Nombre de lignes :", len(lignes))
print("Nombre de mots :", len(mots))
print("Nombre de caractères :", len(texte))
analyse_fichier("[Link]")
TP2.2 – Filtrer des notes
Énoncé : Créer un fichier [Link] contenant uniquement les notes ≥ 10.
Solution :
with open("[Link]", "r") as f:
notes = [int([Link]()) for l in f]
with open("[Link]", "w") as f:
for n in notes:
if n >= 10:
[Link](str(n) + "\n")
TP2.3 – Inverser les lignes
Énoncé : Lire un fichier et créer un nouveau fichier dont les lignes sont inversées.
Solution :
with open("[Link]", "r", encoding="utf-8") as f:
lignes = [Link]()
with open("[Link]", "w", encoding="utf-8") as f:
for ligne in reversed(lignes):
[Link](ligne)
TP3.1 – Ventes (CSV)
Énoncé : Lire un fichier [Link] et calculer le total vendu par produit.
Solution :
import csv
from collections import defaultdict
totaux = defaultdict(int)
with open("[Link]", newline="", encoding="utf-8") as f:
lecteur = [Link](f)
for produit, quantite in lecteur:
totaux[produit] += int(quantite)
print("=== Rapport des ventes ===")
for produit, total in [Link]():
print(produit, ":", total)
TP3.2 – Export vers Excel
Énoncé : Créer un fichier [Link] contenant deux colonnes : Produit – Total.
Solution :
from openpyxl import Workbook
wb = Workbook()
ws = [Link]
[Link] = "Rapport"
[Link](["Produit", "Total"])
for produit, total in [Link]():
[Link]([produit, total])
[Link]("[Link]")
TP3.3 – Moyenne des notes (Excel)
Énoncé : Créer un fichier [Link] contenant les notes et la moyenne en bas.
Solution :
from openpyxl import Workbook
with open("[Link]") as f:
notes = [int([Link]()) for l in f]
wb = Workbook()
ws = [Link]
[Link] = "Notes"
[Link](["Note"])
for n in notes:
[Link]([n])
[Link]([])
[Link](["Moyenne", f"=AVERAGE(A2:A{len(notes)+1})"])
[Link]("[Link]")