Aller au contenu

Introduction à Python

Python est un langage de programmation interprété, polyvalent et facile à apprendre. Il est utilisé dans de nombreux domaines : web, data science, IA, automatisation, etc.

Téléchargez Python depuis python.org ou utilisez winget :

Fenêtre de terminal
winget install Python.Python.3.12
Fenêtre de terminal
# Ubuntu/Debian
sudo apt install python3 python3-pip
# macOS avec Homebrew
brew install python

Créez un fichier hello.py :

# Mon premier programme Python
print("Bonjour, LearnFusion Academy !")

Exécutez-le :

Fenêtre de terminal
python hello.py

Python est un langage à typage dynamique :

# Nombres
age = 25 # int
prix = 19.99 # float
# Chaînes de caractères
nom = "Alice" # str
# Booléens
actif = True # bool
# Listes
notes = [15, 18, 12, 20] # list
# Dictionnaires
etudiant = { # dict
"nom": "Alice",
"age": 25,
"notes": notes
}
age = 18
if age >= 18:
print("Vous êtes majeur")
elif age >= 16:
print("Presque majeur")
else:
print("Vous êtes mineur")
# Boucle for
for i in range(5):
print(f"Itération {i}")
# Boucle while
compteur = 0
while compteur < 5:
print(compteur)
compteur += 1
def calculer_moyenne(notes: list[float]) -> float:
"""Calcule la moyenne d'une liste de notes."""
if not notes:
return 0.0
return sum(notes) / len(notes)
# Utilisation
mes_notes = [15, 18, 12, 20]
moyenne = calculer_moyenne(mes_notes)
print(f"Moyenne : {moyenne}")