557 lines
12 KiB
Plaintext
557 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "701f30c9",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Étape 4: boucle for\n",
|
|
"\n",
|
|
"Ce TP commence par la partie 1. Ensuite les autres parties peuvent se faire dans l'ordre que vous voulez."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9fcf4959",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 1- Faire la même chose plusieurs fois, une belle punition\n",
|
|
"\n",
|
|
"Bob n'a pas été sage, il a encore essayer de scotcher son voisin de classe sur sa chaise. Son professeur a une idée très originale pour le punir : lui faire recopier 100 fois \"je ne dois pas scotcher mon voisin sur sa chaise\". Alors Bob commence"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ce53a518",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(\"je ne dois pas scotcher mon voisin sur sa chaise\")\n",
|
|
"print(\"je ne dois pas scotcher mon voisin sur sa chaise\")\n",
|
|
"print(\"je ne dois pas scotcher mon voisin sur sa chaise\")\n",
|
|
"print(\"je ne dois pas scotcher mon voisin sur sa chaise\")\n",
|
|
"print(\"je ne dois pas scotcher mon voisin sur sa chaise\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "26f634a0",
|
|
"metadata": {},
|
|
"source": [
|
|
"Mais il en marre. Alors il se rappelle qu'il peut sans effort utiliser une boucle `for` pour cela."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e967dc51",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"for i in range(10):\n",
|
|
" print(\"je ne dois pas scotcher mon voisin sur sa chaise\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c4fc909c",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. Modifier le programme précédent pour qu'il réalise la punition de Bob."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a6d6eccb",
|
|
"metadata": {},
|
|
"source": [
|
|
"Bien sûr l'enseignant est fou de rage. Il lui demande de réécrire 100 fois \"Moi Bob, je suis un gros malin et j'utiliser la programmation\" mais cette fois ci entre 2 lignes il doit y avoir une ligne de tirés (\"---------\").\n",
|
|
"\n",
|
|
"2. Réaliser la punition de Bob"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6a64a946",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e313c302",
|
|
"metadata": {},
|
|
"source": [
|
|
"Encore raté, l'enseignant ne sait plus comment construire une vraie bonne punition pour ce plaisantin. Mais il a une idée, il va lui demander de numéroter ses lignes de punition. Il veut des phrases du genre \"C'est la phrase numéro 10, il me reste encore 90 lignes à faire\".\n",
|
|
"\n",
|
|
"Bob se rappelle alors qu'il peut utiliser la variable `i` pour compter le nombre de répétition. Il écrire alors"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "37ef2823",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"for i in range(4):\n",
|
|
" print(\"Je repète \", i, \" fois\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "290aceed",
|
|
"metadata": {},
|
|
"source": [
|
|
"3. Recopier et modifier le programme de Bob pour réaliser sa punition."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1086255e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "331a2cbc",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 2- Dessins ASCII\n",
|
|
"\n",
|
|
"Le but de cette partie est de réaliser des dessins avec des chaines de caractères.\n",
|
|
"\n",
|
|
"Quand on multiplie une chaine de caractères (par exemple \"#\") par un entier (par exemple 4), python va répéter la chaine de caractères.\n",
|
|
"\n",
|
|
"Quelques exemples:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "f853763e",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"####\n",
|
|
"----------\n",
|
|
"___ -----\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"print(\"#\"*4)\n",
|
|
"print(\"-\"*10)\n",
|
|
"print(\"_\"*3, \" \"*2, \"-\"*5)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "39c31073",
|
|
"metadata": {},
|
|
"source": [
|
|
"On peut utiliser des boucles pour faire ensuite des motifs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f27d70f7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"for i in range(5):\n",
|
|
" print(\"-\", \"#\"*i)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1cb955ec",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. Réaliser le motif suivant:\n",
|
|
"\n",
|
|
"\n",
|
|
" -\n",
|
|
" --\n",
|
|
" ---\n",
|
|
" ----\n",
|
|
" -----\n",
|
|
" ------\n",
|
|
" -------\n",
|
|
" --------\n",
|
|
" --------- "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9722d016",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f5b643e3",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. Réaliser le motif suivant\n",
|
|
"\n",
|
|
" \n",
|
|
" ##\n",
|
|
" ####\n",
|
|
" ######\n",
|
|
" ########"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "500f4704",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e3effd90",
|
|
"metadata": {},
|
|
"source": [
|
|
"3. Réaliser le motif suivant\n",
|
|
"\n",
|
|
" ######\n",
|
|
" #####\n",
|
|
" ####\n",
|
|
" ###\n",
|
|
" ##\n",
|
|
" #"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "95855d5a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7a8f1d02",
|
|
"metadata": {},
|
|
"source": [
|
|
"4. Réaliser le motif suivant\n",
|
|
"\n",
|
|
"\n",
|
|
" #\n",
|
|
" ##\n",
|
|
" ###\n",
|
|
" ####\n",
|
|
" #####\n",
|
|
" ######\n",
|
|
" #####\n",
|
|
" ####\n",
|
|
" ###\n",
|
|
" ##\n",
|
|
" #"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2593c488",
|
|
"metadata": {},
|
|
"source": [
|
|
"5. Réaliser le motif suivant\n",
|
|
"\n",
|
|
" # \n",
|
|
" ### \n",
|
|
" ##### \n",
|
|
" ####### "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dee48acd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9880d494",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 2- Calculer des images à pelle\n",
|
|
"\n",
|
|
"On définit la fonction \n",
|
|
"$$ f(x) = 2x - 1$$\n",
|
|
"On veut calculer les valeurs de $f(0)$, $f(1)$ jusqu'à $f(10)$. C'est une tache répétitive,on va pouvoir utiliser une boucle `for`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "795e365f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"for x in range(11):\n",
|
|
" print(\"f(\", x, \")=\", 2*x-1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f7a16c05",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. Calculer les images pour $x$ allant de 0 à 20 par la fonctin $g(x) = 4x^2 - 5x - 1$ (en python la puissance s'écrit avec `**`)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0c00b54a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a7e5a9b6",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. Calculer les images pour $x$ allant de -10 à 0 par la fonctin $h(x) = -x^2 + x - 1$ "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "27810bbd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fa825c38",
|
|
"metadata": {},
|
|
"source": [
|
|
"3. Pour x allant de -100 à 100, quelles sont les valeurs de $x$ qui ont une image positive par la fonction $i(x) = -x^2 + 600$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ca8c089f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fa36a0e2",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 3- Accumulateurs\n",
|
|
"\n",
|
|
"Dans le programme suivant, on veut ajouter tous les nombres de 0 à 100."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a265bcd7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"somme = 0\n",
|
|
"for i in range(101):\n",
|
|
" somme = somme + i\n",
|
|
"print(somme)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5cbbd583",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. Adapter le programme pour qu'il fasse la somme des nombres de 50 à 100."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "cd1beb45",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6d626e06",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. Adapter le programme pour qu'il fasse le produit des tous les nombres de 0 à 100."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4c59373e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "210eecab",
|
|
"metadata": {},
|
|
"source": [
|
|
"3. On veut ajouter tous les nombres de 0 à 100 qui sont paires. Écrire le programme qui correspond."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f41babe3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2d31a788",
|
|
"metadata": {},
|
|
"source": [
|
|
"4. Écrire un programme qui fait la somme de tous les multiples de 5 de 0 à 1000."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "a983409f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bd00940c",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 4- Plusieurs boucles\n",
|
|
"\n",
|
|
"On veut énumérer les points du plan qui ont des coordonnées entières comprises entre -5 et 5. Pour cela il va falloir faire une boucles pour les abscisses (x), une boucle pour les ordonnées (y) et les imbriquer."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "025bd9a2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"for x in range(-5, 6):\n",
|
|
" for y in range(-5, 6):\n",
|
|
" print(\"(\", x, \",\", y,\")\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d99e9ba4",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. Lister tous les points à coordonnées entières qui ont une ordonnée égal à 2 (on note $y = 2$)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2ac7145b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bab46bf6",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. Lister tous les points à coordonnées entières qui sont sur $x = -2$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "4ad1f971",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9d00d050",
|
|
"metadata": {},
|
|
"source": [
|
|
"3. Lister tous les points à coordonnées entières qui sont sur $x = 2y$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c8698a52",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "614e4eec",
|
|
"metadata": {},
|
|
"source": [
|
|
"4. En informatique, on peut coder une couleur en RGB (rouge, vert, bleu). Ce sont 3 nombres compris entre 0 et 255 mis sous forme de coordonnée.\n",
|
|
"\n",
|
|
"Écrire un programme qui liste toutes les couleurs RGB possibles."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "767eb5c6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|