Python 2.7 : Numerical variable

Cette vidéo introduit la manipulation des variables numériques en Python. Jusqu'ici, les variables étaient des chaînes de caractères (str) issues d'input. Maintenant, on découvre comment passer aux entiers (int) et autres types numériques, et surtout comment gérer les conversions entre les deux.

Types de base : str, int, float, bool

Python détermine automatiquement le type d'une variable selon ce qu'on lui affecte. age = 30 crée un int, age = "30" crée un str, age = 30.5 crée un float, et actif = True crée un bool :

nom = "Alice"        # str
age = 30             # int
taille = 1.75        # float
actif = True         # bool

print(type(nom))     # 
print(type(age))     # 

Le piège classique : input retourne toujours une chaîne, même quand l'utilisateur tape un nombre. Donc age = input("Ton âge ? ") donne un str, pas un int. Pour faire des calculs, il faut convertir explicitement avec int(age). À l'inverse, pour concaténer un int dans un message, il faut le convertir en str :

  • int("30") convertit la chaîne "30" en entier 30
  • str(30) convertit l'entier 30 en chaîne "30"
  • float("3.14") convertit en nombre à virgule
  • bool(...) convertit en booléen (True/False)
  • Si la conversion échoue (lettres dans int()), Python lève une ValueError

Pour rendre un programme dynamique, on utilise ces conversions ensemble. Exemple : demander l'âge, le convertir en int, calculer l'âge l'année prochaine, puis le convertir en str pour l'affichage. print("L'an prochain vous aurez " + str(age + 1) + " ans"). Sans la conversion str() autour de age + 1, Python plante car on ne peut pas concaténer une chaîne et un int avec +. La prochaine vidéo détaillera la conversion str → int et la gestion des erreurs de saisie.

Summary

This lesson covers working with numeric variables in Python, including how to declare integers and perform arithmetic operations. The instructor demonstrates type conversions between strings and integers using the str() function, and explains why mixing types (e.g., adding a string "20" to an integer) causes errors. The lesson also introduces other data types like float (decimal numbers) and boolean (True/False), and shows how to make programs dynamic by using variables in calculations.

Key points

  • Numeric variables store integer or float values, and must be converted to strings before concatenating with text using str()
  • The type() function reveals the data type of a variable (int, str, float, bool)
  • Arithmetic operations (+, -, etc.) work directly on numeric types, making programs dynamic and reusable with different input values
  • Python data types include int (whole numbers), str (text), float (decimal numbers), and bool (True/False)
  • Type mismatches cause errors—converting types explicitly using functions like str() and int() is essential
  • Variables combined with arithmetic operators create flexible programs that respond to user input rather than fixed values

FAQ

Why does adding 20 (as a number) and a string in Python cause an error?

Python cannot directly combine different types. If age = 20 (int) and you try to concatenate it with text, Python raises an error because it doesn't know how to add an integer to a string. Use str(age) to convert the number to a string first.

How do you convert a numeric value to text in Python?

Use the str() function. For example, str(20) converts the integer 20 into the string '20'. This allows you to concatenate numbers with text in print statements.

What are the main numeric data types in Python?

The two main numeric types are int (integers like 20, 30) for whole numbers, and float (like 3.14) for numbers with decimal points. Both support arithmetic operations. There is also bool (True/False) for logical values.