🇫🇷 FranceInformatique NSIPremièreLangages & Programmation Python
🐍CH 06📗 NSI Première · 4h/sem

🐍 Langages & Programmation Python

Variables et opérateurs, conditions (if/elif/else), boucles (for/while), fonctions (paramètres, return, docstring), modules, débogage.

🐍 Python — bases
Variables et conditions
Définition
# AFFECTATION
x = 5           # int
nom = 'Alice'   # str
pi = 3.14       # float
ok = True       # bool

# OPÉRATEURS
+ - * /         # arithmétiques standards
//              # division entière : 17//5=3
%               # modulo (reste) : 17%5=2
**              # puissance : 2**10=1024

# CONDITIONS
if note >= 10:
    print('Admis')
elif note >= 8:
    print('Rattrapage')
else:
    print('Refusé')

# OPÉRATEURS COMPARAISON :
== != < > <= >=

# OPÉRATEURS LOGIQUES :
and or not
if a > 0 and a < 10:
    print('Entre 0 et 10')
Boucles for et while
Notion clé
# BOUCLE for (nombre d'itérations connu)
for i in range(5):      # 0,1,2,3,4
    print(i)

for i in range(1, 11, 2):  # 1,3,5,7,9
    print(i)

for x in ['a','b','c']:  # itérer une liste
    print(x)

for i, x in enumerate(['a','b','c']):
    print(i, x)  # 0 a | 1 b | 2 c

# BOUCLE while (condition)
n = 10
while n > 0:
    print(n)
    n = n // 2  # 10,5,2,1

# CONTRÔLE
break     : sortir immédiatement de la boucle
continue  : passer à l'itération suivante

# Compréhension de liste
carres = [x**2 for x in range(5)]  # [0,1,4,9,16]
pairs = [x for x in range(10) if x%2==0]
Exercices
EX-PY1FacileFonctions Python simples

Écrire en Python : 1. est_pair(n) : True si n est pair 2. maximum(a,b,c) : maximum de 3 nombres 3. compte_mots(phrase) : nb de mots

🤖 Résoudre avec IA
← Précédent
Architecture & OS
Suivant →
Algorithmique