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.
Installation
Section intitulée « Installation »Téléchargez Python depuis python.org ou utilisez winget :
winget install Python.Python.3.12Linux/macOS
Section intitulée « Linux/macOS »# Ubuntu/Debiansudo apt install python3 python3-pip
# macOS avec Homebrewbrew install pythonPremier programme
Section intitulée « Premier programme »Créez un fichier hello.py :
# Mon premier programme Pythonprint("Bonjour, LearnFusion Academy !")Exécutez-le :
python hello.pyVariables et types de données
Section intitulée « Variables et types de données »Python est un langage à typage dynamique :
# Nombresage = 25 # intprix = 19.99 # float
# Chaînes de caractèresnom = "Alice" # str
# Booléensactif = True # bool
# Listesnotes = [15, 18, 12, 20] # list
# Dictionnairesetudiant = { # dict "nom": "Alice", "age": 25, "notes": notes}Structures de contrôle
Section intitulée « Structures de contrôle »Conditions
Section intitulée « Conditions »age = 18
if age >= 18: print("Vous êtes majeur")elif age >= 16: print("Presque majeur")else: print("Vous êtes mineur")# Boucle forfor i in range(5): print(f"Itération {i}")
# Boucle whilecompteur = 0while compteur < 5: print(compteur) compteur += 1Fonctions
Section intitulée « Fonctions »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)
# Utilisationmes_notes = [15, 18, 12, 20]moyenne = calculer_moyenne(mes_notes)print(f"Moyenne : {moyenne}")