Echauffement
Manipulation de tableau
On rappelle que la fonction randint(a, b) du module random retourne un nombre entier aléatoire compris entre a et b (b compris).
-
Écrire une fonction
lancer_trois_des
permettant de retourner une liste de 3 nombres entiers simulant le lancer de 3 dés cubiques. -
On appelle cette fonction avec l'instruction suivante :
📋 TexteQuel est le type de la variable mon_lancer ?mon_lancer = lancer_trois_des()
-
Quelle instruction permet alors de calculer la somme des trois dés ?
- somme = mon_lancer[1] + mon_lancer[2] + mon_lancer[3]
- somme = mon_lancer[0] + mon_lancer[1] + mon_lancer[2]
- somme = mon_lancer + mon_lancer + mon_lancer
-
On appelle cette fonction avec l'instruction suivante :
de_1, de_2, de_3 = lancer_trois_des()
Cette instruction génère-t-elle une erreur ?
Si oui, pourquoi ? Si non, donner une instruction permettant d'affecter à une variable la somme des trois dés.
-
Question 1 :
🐍 Script Python 1 2 3 4 5 6 7
import random def lancer_trois_des(): a = random.randint(1, 6) b = random.randint(1, 6) c = random.randint(1, 6) return a, b, c
-
c'est une liste
-
- somme = mon_lancer[1] + mon_lancer[2] + mon_lancer[3]
- somme = mon_lancer[0] + mon_lancer[1] + mon_lancer[2]
- somme = mon_lancer + mon_lancer + mon_lancer
- Non mais on peut écrire
somme = de_1 + de_2 + de_3
Manipulation de tableau
On considère la suite d'instructions données ci-contre. Donner l'état des deux tableaux à la fin de la suite d'instructions.
tab_x = [7, 77, 777, 7777]
tab_y = [5, 55, 555, 5555]
tab_x[2] = tab_y[1]
tab_y[3] = tab_x[0]
tab_x[1] = tab_x[2]
tab_y[1] = tab_y[3]
tab_x vaut [7, 55, 55, 7777]
tab_y vaut [5, 7, 555, 7]
Manipulation de tableau
On considère la suite d'instructions données ci-contre. Donner les valeurs de chacune des variables a à f.
🐍 Script Python | |
---|---|
1 2 3 4 5 6 7 8 |
|
🐍 Script Python | |
---|---|
1 2 3 4 5 6 |
|
Manipulation de tableau
Pour chacune des instructions ci-dessous, écrire le tableau qui a été créé.
🐍 Script Python | |
---|---|
1 2 3 4 |
|
🐍 Script Python | |
---|---|
1 2 3 4 |
|
Manipulation de tableau
On rappelle que la fonction len() permet de retourner la longueur d'un tableau mais aussi la longueur d'une chaîne de caractères.
- Combien de noms vont être affichés à l'issue de la séquence d'instructions ci-dessous ?
🐍 Script Python 1 2 3 4
grands_noms = ["Lovelace", "Clarke", "Goldstine", "Hopper", "Recoque", "Hamilton"] for nom in grands_noms: if "o" in nom: print(nom)
- Quelle sera la valeur de quantite_mystere à la fin de la sequence d'instructions ci-dessous
🐍 Script Python 1 2 3 4
grands_noms = ["Lovelace", "Clarke", "Goldstine", "Hopper", "Recoque", "Hamilton"] quantite_mystere = 0 for nom in grands_noms: quantite_mystere = quantite_mystere + len(nom)
- Cinq. Tout ceux qui comporte un O dans leur nom
- 44 (somme des nombres de caractères de chacun des noms)
Manipulation de tableau
On représente la matrice donnée ci-contre grâce au tableau ma_belle_matrice.
🐍 Script Python | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
- Donner les valeurs de chacune des variables a, b, c et d
- Proposer une suite d'instructions permettant de modifier
ma_belle _matrice
afin qu'elle corresponde à la matrice donnée ci-contre.a b X d X X X X i j X l m n X p q r X t
-
a vaut ['m', 'n', 'o', 'p']
b vaut o, c vaut g, d vaut j -
une possibilité est
🐍 Script Python | |
---|---|
1 2 3 4 5 |
|
Manipulation de tableau
Colorier les bonnes cases en fonction de l'algorithme proposé
-
Cas n°1
🐍 Script Python 1 2 3
for i in range(0, 8): for j in range(0, i): M[i][j] = 'X'
-
Cas n°2
🐍 Script Python 1 2 3
for i in range(0, 8): for j in range(i+1, 8): M[i][j] = 'X'
-
Cas n°3
🐍 Script Python 1 2 3
for i in range(1, 8): for j in range(0, 8-i): M[i][j] = 'X'
-
Cas n°4
🐍 Script Python 1 2 3
for i in range(0, 8): for j in range(3, 5): M[i][j] = 'X'
-
Cas n°5
🐍 Script Python 1 2 3
for i in range(3, 5): for j in range(1, 7): M[i][j] = 'X'
-
Cas n°6
🐍 Script Python 1 2 3
for i in range(1, 8): for j in range(8-i, 8): M[i][j] = 'X'