TD1 :python
Ex1 :
# Liste des expressions données dans l'exercice
expressions = [
"7 + 36",
"(3 + 4) ** 3",
"36 - 5",
"(1 + 2) * 5",
"(2 + 1 ** 8) ** 7"
]
print("Résultats des expressions et test Friedman :\n")
for expr in expressions:
try:
result = eval(expr) # Évalue l'expression
digits_expr = sorted([ch for ch in expr if [Link]()])
digits_result = sorted(list(str(result)))
# Affichage
print(f"{expr} = {result}")
if digits_expr == digits_result:
print("→ C'est un nombre de Friedman \n")
else:
print("→ Ce n'est pas un nombre de Friedman \n")
except Exception as e:
print(f"Erreur dans l'expression {expr} : {e}")
ex2 :
1. (1 + 2) ** 3
Prédiction :
→ (3) ** 3 = 27
✅ Résultat : 27 (car 1 + 2 = 3, et 3³ = 27)
🔠 2. "Da" * 4
Prédiction :
→ "DaDaDaDa" (la chaîne est répétée 4 fois)
✅ Résultat : "DaDaDaDa"
❌ 3. "Da" + 3
Prédiction :
→ Erreur ! On ne peut pas ajouter une chaîne (str) et un entier (int)
❌ Résultat :
python
CopierModifier
TypeError: can only concatenate str (not "int") to str
🔤 4. ("Pa" + "La") * 2
Prédiction :
→ "PaLaPaLa"
✅ Résultat : "PaLaPaLa"
("Pa" + "La" donne "PaLa", puis multiplié par 2)
❌ 5. ("Da" * 4) / 2
Prédiction :
→ Erreur ! On ne peut pas diviser une chaîne
❌ Résultat :
python
CopierModifier
TypeError: unsupported operand type(s) for /: 'str' and 'int'
➗ 6. 5 / 2
Prédiction :
→ 2.5 (division réelle)
✅ Résultat : 2.5
➗ 7. 5 // 2
Prédiction :
→ 2 (division entière)
✅ Résultat : 2
🔁 8. 5 % 2
Prédiction :
→ 1 (reste de la division de 5 par 2)
✅ Résultat : 1
Ex3 :
1. str(4) * int("3")
Analyse :
str(4) → "4" (convertit 4 en chaîne de caractères)
int("3") → 3 (convertit "3" en entier)
"4" * 3 → "444" (répète la chaîne "4" trois fois)
✅ Résultat : "444"
➕ 2. int("3") + float("3.2")
Analyse :
int("3") → 3
float("3.2") → 3.2
3 + 3.2 → 6.2 (addition d’un int + float → résultat en float)
✅ Résultat : 6.2
❌ 3. str(3) * float("3.2")
Analyse :
str(3) → "3" (une chaîne)
float("3.2") → 3.2 (un nombre décimal)
"3" * 3.2 → ❌ Erreur : on ne peut pas multiplier une chaîne par un float
❌ Résultat :
python
CopierModifier
TypeError: can't multiply sequence by non-int of type 'float'
🔁 4. str(3/4) * 2
Analyse :
3 / 4 → 0.75
str(0.75) → "0.75"
"0.75" * 2 → "0.750.75" (chaîne répétée deux fois)
✅ Résultat : "0.750.75"
Ex4 :
Ex5 :
Ex6 :
Ex7 :
Ex8 :
Ex9 :
Ex10 :
Ex11 :
Ex12 :