Les types de base
Python a cinq types fondamentaux :
str— du texte :"Bonjour"ou'Salut'int— un nombre entier :42,-7float— un nombre décimal :3.14,-0.5bool— vrai ou faux :True,False(avec majuscule !)None— « rien, pas de valeur »
La fonction type() vous dit le type d'une valeur :
print(type("Bonjour")) # <class 'str'>
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
f-strings — le format moderne
Les f-strings (Python 3.6+) sont la façon la plus pratique d'insérer des variables dans du texte :
nom = "Alice"
age = 25
# f-string : f devant les guillemets
print(f"Bonjour {nom}, vous avez {age} ans")
# Bonjour Alice, vous avez 25 ans
# On peut même faire des calculs
print(f"L'année prochaine, {nom} aura {age + 1} ans")
# L'année prochaine, Alice aura 26 ans
L'ancienne manière avec + (concaténation) fonctionne mais est moins lisible :
# Ancien style (à éviter)
print("Bonjour " + nom + ", vous avez " + str(age) + " ans")
Conversion de types
Parfois il faut convertir un type en un autre. Python fournit des fonctions simples :
# String vers nombre
prix = "49.99"
prix_num = float(prix) # 49.99
quantite = "3"
quantite_num = int(quantite) # 3
# Nombre vers string
age = 25
texte = str(age) # "25"
# Calcul
total = prix_num * quantite_num
print(f"Total : {total} €") # Total : 149.97 €
Erreur fréquente : int("3.14") plante ! Il faut d'abord passer par float(), puis int() si on veut un entier.
input() — demander une saisie
input() affiche un message et attend que l'utilisateur tape quelque chose :
nom = input("Comment vous appelez-vous ? ")
print(f"Bienvenue, {nom} !")
age = input("Quel âge avez-vous ? ")
age = int(age) # input() renvoie TOUJOURS un string
print(f"Dans 10 ans, vous aurez {age + 10} ans")
Résultat (si l'utilisateur tape "Alice" puis "25") :
Comment vous appelez-vous ? Alice
Bienvenue, Alice !
Quel âge avez-vous ? 25
Dans 10 ans, vous aurez 35 ans
Piège classique : input() renvoie toujours un str. Si vous oubliez int(), le calcul "25" + 10 provoque une erreur TypeError.
Basic types
Python has five fundamental types:
str— text:"Hello"or'Hi'int— whole number:42,-7float— decimal number:3.14,-0.5bool— true or false:True,False(capitalized!)None— "nothing, no value"
The type() function tells you the type of a value:
print(type("Hello")) # <class 'str'>
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
f-strings — the modern format
f-strings (Python 3.6+) are the most practical way to insert variables into text:
name = "Alice"
age = 25
# f-string: f before the quotes
print(f"Hello {name}, you are {age} years old")
# Hello Alice, you are 25 years old
# You can even do calculations
print(f"Next year, {name} will be {age + 1}")
# Next year, Alice will be 26
The old way with + (concatenation) works but is less readable:
# Old style (avoid)
print("Hello " + name + ", you are " + str(age) + " years old")
Type conversion
Sometimes you need to convert one type to another. Python provides simple functions:
# String to number
price = "49.99"
price_num = float(price) # 49.99
quantity = "3"
quantity_num = int(quantity) # 3
# Number to string
age = 25
text = str(age) # "25"
# Calculation
total = price_num * quantity_num
print(f"Total: {total} EUR") # Total: 149.97 EUR
Common error: int("3.14") crashes! You need to use float() first, then int() if you want a whole number.
input() — asking for user input
input() displays a message and waits for the user to type something:
name = input("What is your name? ")
print(f"Welcome, {name}!")
age = input("How old are you? ")
age = int(age) # input() ALWAYS returns a string
print(f"In 10 years, you will be {age + 10}")
Result (if the user types "Alice" then "25"):
What is your name? Alice
Welcome, Alice!
How old are you? 25
In 10 years, you will be 35
Classic trap: input() always returns a str. If you forget int(), the calculation "25" + 10 triggers a TypeError.
Copiez ce prompt dans Claude ou ChatGPT :
Écris un script Python qui demande le prénom et l'âge de l'utilisateur, puis affiche un message personnalisé avec une f-string. Ajoute une gestion d'erreur si l'âge n'est pas un nombre.
True en Python ?nom dans une chaîne de caractères ?input() en Python ?