Programmation avec Python : les bases
Réactive les bases de la programmation avec en t'exerçant sur la plateforme
CodEx.
Rituels
Niveau Seconde
flowchart TD
appel_fonction["Appels de fonctions"]:::normal --> variable["Variables et affectations"]:::normal
variable --> instruction_conditionnelle["Instructions conditionnelles"]:::normal
instruction_conditionnelle --> boucle_bornee["Boucles bornées"]:::normal
boucle_bornee --> fonction["Fonctions"]:::normal
fonction --> tableau["Tableaux"]:::normal
click appel_fonction href "https://codex.forge.apps.education.fr/exercices/alien_1/" " "
click variable href "https://codex.forge.apps.education.fr/exercices/alien2/" " "
click instruction_conditionnelle href "https://codex.forge.apps.education.fr/exercices/alien3/" " "
click boucle_bornee href "https://codex.forge.apps.education.fr/exercices/alien4/" " "
click fonction href "https://codex.forge.apps.education.fr/exercices/alien5/" " "
click tableau href "https://codex.forge.apps.education.fr/exercices/alien6/" " "
classDef normal fill:#ffffff, stroke:black
classDef validation fill:#ff880020, stroke:orange, font-color:orange
classDef trait_validation stroke:orange
classDef terminaison fill:#0088ff20, stroke: #0088ff
Niveau Première
Manipulation de tableaux
Parcours arborescent
flowchart TD
tab_0["Somme d'une liste d'entiers"]:::normal --> tab_1["Moyenne simple"]:::normal
tab_1 t0@-.-> tab_2["Moyenne pondérée"]:::validation
tab_1---> fin:::terminaison@{ shape: dbl-circ, label: "Fin" }
click tab_0 href "https://codex.forge.apps.education.fr/exercices/somme_entiers/" " "
click tab_1 href "https://codex.forge.apps.education.fr/exercices/moyenne/" " "
click tab_2 href "https://codex.forge.apps.education.fr/exercices/moy_ponderee/" " "
classDef normal fill:#ffffff, stroke:black
classDef validation fill:#ff880020, stroke:orange, font-color:orange
classDef trait_validation stroke:orange
classDef terminaison fill:#0088ff20, stroke: #0088ff
class t0 trait_validation
class t1 trait_validation
Objectifs d'apprentissage
Itérer sur les éléments d'un tableau ;
lire et modifier les éléments d’un tableau grâce à leurs index ;
utiliser un accumulateur pour mettre à jour la valeur d'une variable à chaque itération.
Dictionnaires
Parcours arborescent
flowchart TD
dic_0["Couleurs en HTML"]:::normal --> dic_1["Décoder le 'Mra bs Clguba'"]:::normal
dic_1 t0@-.-> dic_4["Top-Likes"]:::validation
dic_1 ---> dic_2["Dictionnaire de valeurs extrêmes"]:::normal
dic_2 --> dic_3["Dictionnaire d'occurrences"]:::normal
dic_3 t1@-.-> dic_5["Union de dictionnaires"]:::validation
dic_3 ---> fin:::terminaison@{ shape: dbl-circ, label: "Fin" }
click dic_0 href "https://codex.forge.apps.education.fr/exercices/couleurs/" " "
click dic_1 href "https://codex.forge.apps.education.fr/exercices/mra_bs_clguba/" " "
click dic_4 href "https://codex.forge.apps.education.fr/exercices/top_like/" " "
click dic_2 href "https://codex.forge.apps.education.fr/exercices/dict_extremes/" " "
click dic_3 href "https://codex.forge.apps.education.fr/exercices/dico_occurrences/" " "
click dic_5 href "https://codex.forge.apps.education.fr/exercices/union_dictionnaires/" " "
classDef normal fill:#ffffff, stroke:black
classDef validation fill:#ff880020, stroke:orange, font-color:orange
classDef trait_validation stroke:orange
classDef terminaison fill:#0088ff20, stroke: #0088ff
class t0 trait_validation
class t1 trait_validation
Objectifs d'apprentissage
Manipuler les éléments d'un dictionnaire : accès, modification, ajout, suppression ;
itérer sur les éléments d'un dictionnaire avec les méthodes
keys(), values() et items().
Chaînes de caractères
Parcours arborescent
flowchart TD
chaine_0["Occurrences d'un caractère dans un mot"]:::normal --> chaine_1["Validation de mot de passe"]:::normal
chaine_1 t0@-.-> chaine_4["Mots qui se correspondent"]:::validation
chaine_1 ---> chaine_2["Élocution chez le dentiste"]:::normal
chaine_2 t1@-.-> chaine_5["Découpage de mots"]:::validation
chaine_2 --> chaine_6["Score d'un mot au Scrabble"]:::normal
chaine_6 t2@-.-> chaine_3["Chiffre de César"]:::validation
chaine_6 ---> fin:::terminaison@{ shape: dbl-circ, label: "Fin" }
click chaine_0 href "https://codex.forge.apps.education.fr/exercices/cpt_occurrences/" " "
click chaine_1 href "https://codex.forge.apps.education.fr/exercices/validation_mdp/" " "
click chaine_2 href "https://codex.forge.apps.education.fr/exercices/dentiste/" " "
click chaine_3 href "https://codex.forge.apps.education.fr/exercices/code_cesar/" " "
click chaine_4 href "https://codex.forge.apps.education.fr/exercices/correspond_mot/" " "
click chaine_5 href "https://codex.forge.apps.education.fr/exercices/split/" " "
click chaine_6 href "https://codex.forge.apps.education.fr/exercices/scrabble_1/" " "
classDef normal fill:#ffffff, stroke:black
classDef validation fill:#ff880020, stroke:orange, font-color:orange
classDef trait_validation stroke:orange
classDef terminaison fill:#0088ff20, stroke: #0088ff
class t0 trait_validation
class t1 trait_validation
class t2 trait_validation
Objectifs d'apprentissage
Manipuler les chaînes de carcatères ;
compter les occurrences d'un caractère ;
comparer deux chaines de caractères.
Expression booléenne
Parcours arborescent
flowchart TD
direction TB
bool_0["Autour des booléens"]:::normal --> bool_1["Opérateurs booléens"]:::normal
bool_1 --> bool_2["Années bissextiles"]:::normal
bool_2 --> fin:::terminaison@{ shape: dbl-circ, label: "Fin" }
click bool_0 href "https://codex.forge.apps.education.fr/exercices/autour_des_booleens/" " "
click bool_1 href "https://codex.forge.apps.education.fr/exercices/operateurs_booleens/" " "
click bool_2 href "https://codex.forge.apps.education.fr/exercices/bissextile/" " "
classDef normal fill:#ffffff, stroke:black
classDef validation fill:#ff880020, stroke:orange, font-color:orange
classDef trait_validation stroke:orange
classDef terminaison fill:#0088ff20, stroke: #0088ff
class t0 trait_validation
class t1 trait_validation
class t2 trait_validation
Objectifs d'apprentissage
Former des expressions booléenne à l'aide des opérateur
not, and, or ;
Connaître les tables de vérité des opérateurs booléens ;
utiliser les instructions conditionnelles imbriquées.
Tableaux (suite)
Parcours arborescent
flowchart TD
liste_0["Fonctions simples"]:::normal --> liste_1["Autour de range"]:::normal
liste_1 --> liste_2["La course cycliste"]:::normal
liste_1 ---> liste_3["Indice ou valeur?"]:::normal
liste_1 ----> liste_4["Listes des termes d'une suite mathématiques"]:::normal
liste_1 -----> liste_5["Listes des termes d'une suite arithmétique"]:::normal
liste_1 t0@-.....-> liste_6["Filter un tableau"]:::validation
click liste_0 href "https://codex.forge.apps.education.fr/exercices/fonctions/" " "
click liste_1 href "https://codex.forge.apps.education.fr/exercices/range/" " "
click liste_2 href "https://codex.forge.apps.education.fr/exercices/course_cycliste/" " "
click liste_3 href "https://codex.forge.apps.education.fr/exercices/indices_valeurs/" " "
click liste_4 href "https://codex.forge.apps.education.fr/exercices/termes_d_une_suite/" " "
click liste_5 href "https://codex.forge.apps.education.fr/en_travaux/suite_arithm/" " "
click liste_6 href "https://codex.forge.apps.education.fr/exercices/filtre_tableau/" " "
classDef normal fill:#ffffff, stroke:black
classDef validation fill:#ff880020, stroke:orange, font-color:orange
classDef trait_validation stroke:orange
classDef terminaison fill:#0088ff20, stroke: #0088ff
class t0 trait_validation
Objectifs d'apprentissage
Itérer sur les éléments d'un tableau par indice ou par valeur;
construire un tableau en compréhension.
Correction
Manipulation de tableaux
Somme d'une liste d'entiers
def somme_valeurs(tableau):
somme = 0
for valeur in tableau:
somme = somme + valeur
return somme
Moyenne simple
def moyenne(valeurs):
effectif = len(valeurs)
somme_valeurs = 0
for a in valeurs:
somme_valeurs += a
return somme_valeurs / effectif
Moyenne pondérée
def moyenne(notes_ponderees):
cumul_points = 0.0
cumul_coeffs = 0
for (note, coeff) in notes_ponderees:
cumul_points += coeff * note
cumul_coeffs += coeff
return cumul_points / cumul_coeffs
Dictionnaire
Couleurs en HTML
HEX_DEC = {
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"A": 10,
"B": 11,
"C": 12,
"D": 13,
"E": 14,
"F": 15,
}
def hex_int(seizaine, unite):
return HEX_DEC[seizaine] * 16 + HEX_DEC[unite]
def html_vers_rvb(html):
return (
hex_int(html[1], html[2]),
hex_int(html[3], html[4]),
hex_int(html[5], html[6]),
)
Décoder le "Mra bs Clguba"
def decode(s, d):
resultat = ""
for caractere in s:
if caractere in d:
caractere = d[caractere]
resultat += caractere
return resultat
Dictionnaire de valeurs extrêmes
def extremes(valeurs):
if valeurs == []:
return {"min": None, "max": None}
else:
mini = valeurs[0]
maxi = valeurs[0]
for x in valeurs:
if x < mini:
mini = x
if x > maxi:
maxi = x
return {"min": mini, "max": maxi}
Dictionnaire d'occurrences
def occurrence_caracteres(phrase):
occurrences = dict()
for caractere in phrase:
if caractere in occurrences:
occurrences[caractere] += 1
else:
occurrences[caractere] = 1
return occurrences
Top-Likes
def top_likes(likes):
max_pseudo = ""
max_likes = -1
for pseudo in likes:
if likes[pseudo] > max_likes:
max_pseudo = pseudo
max_likes = likes[pseudo]
elif likes[pseudo] == max_likes and pseudo < max_pseudo:
max_pseudo = pseudo
return (max_pseudo, max_likes)
Union de dictionnaires
def union(d1, d2):
d = {}
for cle in d1:
d[cle] = d1[cle]
for cle in d2:
if cle not in d:
d[cle] = d2[cle]
else:
d[cle] = d[cle] + d2[cle]
return d
Chaînes de caractères
Occurrences d'un caractère dans un mot
def compte_occurrences(cible, mot):
nb_occurrences = 0
for lettre in mot:
if lettre == cible:
nb_occurrences += 1
return nb_occurrences
Validation de mot de passe
SPECIAUX = ",;:!?./§%*$£&#{}()[]-_@"
def valide(mdp):
if len(mdp) < 14:
return False
minuscule = False
majuscule = False
chiffre = False
special = False
for c in mdp:
if c.isupper():
majuscule = True
elif c.islower():
minuscule = True
elif c.isnumeric():
chiffre = True
elif c in SPECIAUX:
special = True
return minuscule and majuscule and chiffre and special
VOYELLES = ["a", "e", "i", "o", "u", "y"]
Élocution chez le dentiste
def dentiste(texte):
resultat = ""
for lettre in texte:
if lettre in VOYELLES:
resultat = resultat + lettre
return resultat
Score d'un mot au scrable
VALEURS = {
"A": 1,
"B": 3,
"C": 3,
"D": 2,
"E": 1,
"F": 4,
"G": 2,
"H": 4,
"I": 1,
"J": 8,
"K": 10,
"L": 1,
"M": 2,
"N": 1,
"O": 1,
"P": 3,
"Q": 8,
"R": 1,
"S": 1,
"T": 1,
"U": 1,
"V": 4,
"W": 10,
"X": 10,
"Y": 10,
"Z": 10,
}
def score_scrabble(mot):
score = 0
for lettre in mot:
score += VALEURS[lettre]
return score
Expression booléenne
Autour des booléens
# Exemple donné de traduction de condition
# prop_1 : "3 est strictement inférieur à 4"
prop_1 = 3 < 4
# prop_2 : "3 est supérieur ou égal à 4"
prop_2 = 3 >= 4
# prop_3 : "le caractère 'n' est dans la chaine 'bonjour'"
prop_3 = "n" in "bonjour"
# prop_4 : "Le calcul 3 + 5 * 2 vaut 16"
prop_4 = 3 + 5 * 2 == 16
# prop_5 : "5 est un nombre pair"
prop_5 = 5 % 2 == 0
# prop_6 : "12 est dans la liste [k for k in range(12)]"
prop_6 = 12 in [k for k in range(12)]
# prop_7 : "'ju' n'est pas dans 'bonjour'"
prop_7 = "ju" not in "bonjour"
# prop_8 : "3 est égal à 1 + 2 et 'a' est dans 'Boole'"
prop_8 = (3 == 1 + 2) and ("a" in "Boole")
# prop_9 : "3 est égal à 1 + 2 ou 'a' est dans 'Boole'"
prop_9 = (3 == 1 + 2) or ("a" in "Boole")
# prop_10 : "6 est un nombre pair et un multiple de 3"
prop_10 = (6 % 2 == 0) and (6 % 3 == 0)
# prop_11 : "648 est dans les tables de 2, de 3 et de 4"
prop_11 = ((648 % 2 == 0) and (648 % 3 == 0)) and (648 % 4 == 0)
# prop_12 : "25 est strictement compris entre 24 et 26"
prop_12 = (25 > 24) and (25 < 26)
# prop_12 : "ou"
prop_12 = 24 < 25 < 26
# prop_13 : "'a' est dans 'hello' ou dans 'hallo'"
prop_13 = ("a" in "hello") or ("a" in "hallo")
# prop_14 : "8 est égal à 4 * 2 et différent de 9 - 1"
prop_14 = (8 == 4 * 2) and (8 != 9 - 1)
# prop_15 : "7 est entre 1 et 5 (inclus l'un et l'autre) ou strictement supérieur à 6"
prop_15 = (7 >= 1) and (7 <= 5) or (7 >= 6)
# prop_15 : "ou"
prop_15 = (1 <= 7 <= 5) or (7 >= 6)
# prop_16: "5 est strictement compris entre 7 ou 8 est strictement inférieur à 9"
prop_16 = (7 < 5 and 5 < 8) or 8 < 9
# prop_17: "7 est strictement inférieur à 5 et ...
# ...(8 est strictement supérieur à 5 ou strictment inférieur à 9)"
prop_17 = 7 < 5 and (5 < 8 or 8 < 9)
Opérateurs booléens
def non(a):
if a:
return False
else:
return True
def et(a, b):
if a:
if b:
return True
return False
def ou(a, b):
if a:
return True
elif b:
return True
return False
def ou_exclusif(a, b):
return ou(et(a, non(b)), et(non(a), b))
def non_ou(a, b):
return non(ou(a, b))
def non_et(a, b):
return non(et(a, b))
Années bissextiles
def est_bissextile(annee):
bis = False
if annee%4 == 0:
if annee%100 != 0:
bis = True
else:
if annee%400 == 0:
bis = True
return bis
Tableaux (suite)
La course cyliste
def nombre_coureurs(classement):
return len(classement)
def premier(classement):
return classement[0]
def dernier(classement):
return classement[len(classement) - 1]
def coureur_en_position(classement, position):
return classement[position - 1]
Indice ou valeur?
def somme_par_indices_pairs(nombres):
somme = 0
for i in range(len(nombres)):
if i % 2 == 0:
somme += nombres[i]
return somme
def somme_des_valeurs_paires(nombres):
somme = 0
for x in nombres:
if x % 2 == 0:
somme += x
return somme
Listes des termes d'une suite mathématique
def liste_termes(N):
termes = [0 for k in range(N)]
termes[0] = 0
termes[1] = 1
for n in range(2, N):
termes[n] = 5 * termes[n - 1] - 8 * termes[n - 2]
return termes
Listes des termes d'une suite arithmétique
def arithmetique(N):
return [5 + 10 * n for n in range(N)]
Filtrer un tableau
def filtre(f, nombres):
return [x for x in nombres if f(x)]
Cours
Synthèse de cours
Cours/TP
-
Tuples
-
Dictionnaires
-
Tableaux
Exercices
-
Dictionnaires
-
Tableaux
version CAPYTALE