Programmes Python - Exercices
1. Affichage personnalisé
nom = input("Entrez votre nom : ")
prenom = input("Entrez votre prénom : ")
age = input("Entrez votre âge : ")
print(f"Bonjour {prenom} {nom}, vous avez {age} ans !")
2. Mini-calculatrice
a = float(input("Entrez le premier nombre : "))
b = float(input("Entrez le deuxième nombre : "))
print("1- Addition")
print("2- Soustraction")
print("3- Multiplication")
print("4- Division")
choix = int(input("Votre choix : "))
if choix == 1:
print("Résultat =", a + b)
elif choix == 2:
print("Résultat =", a - b)
elif choix == 3:
print("Résultat =", a * b)
elif choix == 4:
if b != 0:
print("Résultat =", a / b)
else:
print("Erreur : division par zéro !")
else:
print("Choix invalide")
3. Pair ou impair
n = int(input("Entrez un entier : "))
if n % 2 == 0:
print("Le nombre est pair.")
else:
print("Le nombre est impair.")
4. Boucles et factorielle
a. Somme de 1 à N
N = int(input("Entrez N : "))
somme = 0
for i in range(1, N + 1):
somme += i
print("Somme =", somme)
b. Factorielle
N = int(input("Entrez un nombre : "))
fact = 1
for i in range(1, N + 1):
fact *= i
print("Factorielle =", fact)
5. Somme des entiers de 1 à N (formule)
N = int(input("Entrez N : "))
print("Somme =", N * (N + 1) // 2)
6. Fibonacci (15 termes)
a, b = 0, 1
for _ in range(15):
print(a, end=" ")
a, b = b, a + b
7. Table de multiplication
n = int(input("Entrez un nombre : "))
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
8. Somme des entiers pairs
N = int(input("Entrez N : "))
s=0
for i in range(2, N + 1, 2):
s += i
print("Somme des pairs =", s)
9. Jeu du nombre secret
import random
secret = [Link](1, 20)
guess = 0
while guess != secret:
guess = int(input("Devinez le nombre (1 à 20) : "))
if guess < secret:
print("Trop petit !")
elif guess > secret:
print("Trop grand !")
else:
print("Bravo ! Vous avez trouvé !")
ÉNONCÉ 2
1. Maximum de deux variables
a = float(input("Entrez a : "))
b = float(input("Entrez b : "))
print("Le maximum est :", max(a, b))
2. Majeur ou mineur
age = int(input("Entrez votre âge : "))
if age >= 18:
print("Vous êtes majeur !")
else:
print("Vous êtes mineur !")
3. Diviseurs d’un nombre
n = int(input("Entrez un entier : "))
print("Diviseurs :")
for i in range(1, n + 1):
if n % i == 0:
print(i)
4. Surface et périmètre d’un cercle
import math
r = float(input("Entrez le rayon : "))
surface = [Link] * r * r
perimetre = 2 * [Link] * r
print("Surface =", surface)
print("Périmètre =", perimetre)
5. Division euclidienne
a = int(input("Entrez a : "))
b = int(input("Entrez b : "))
if b != 0:
print("Quotient =", a // b)
print("Reste =", a % b)
else:
print("Erreur : division par zéro !")
6. Carré parfait
import math
n = int(input("Entrez un nombre : "))
if int([Link](n)) ** 2 == n:
print("C’est un carré parfait.")
else:
print("Ce n’est pas un carré parfait.")
7. Test de primalité
n = int(input("Entrez un entier : "))
if n < 2:
print("Pas premier")
else:
premier = True
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
premier = False
break
if premier:
print("Nombre premier")
else:
print("Pas premier")
8. Évaluation d’une note
note = float(input("Entrez une note entre 0 et 20 : "))
if note < 10:
print("Non validé")
elif note < 12:
print("Passable")
elif note < 14:
print("Assez bien")
elif note < 16:
print("Bien")
elif note < 18:
print("Très bien")
elif note <= 20:
print("Excellent")
else:
print("Note invalide")