567 lines
13 KiB
Plaintext
567 lines
13 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Mémo Python\n",
|
||
|
"\n",
|
||
|
"[Python par Repl.it](#Avec-Repl.it)\n",
|
||
|
"\n",
|
||
|
"-----------\n",
|
||
|
"Notions de programmation\n",
|
||
|
"* [input](#input)\n",
|
||
|
"* [Les conditions if](#Condition-if)\n",
|
||
|
"* [Les boucles for](#Boucles-for)\n",
|
||
|
"* [Les boucles while](#Boucles-while)\n",
|
||
|
"* [Les fonctions](#Fonctions)\n",
|
||
|
"------------\n",
|
||
|
"Ressouces Python\n",
|
||
|
"* [site du zero](http://openclassrooms.com/courses/apprenez-a-programmer-en-python) (cours en ligne)\n",
|
||
|
"* [codeacademy](http://www.codecademy.com/) (cours en ligne)\n",
|
||
|
"* [code conbat](http://codecombat.com) (jeu en ligne qui se joue en codant)\n",
|
||
|
"* [checkio](http://www.checkio.org/) (jeu en ligne - anglais - avec beaucoup de langage différents)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Avec Repl.it\n",
|
||
|
"\n",
|
||
|
"Se rendre sur le site de [repl.it](https://repl.it/repls/SpiffyOpulentBackend). \n",
|
||
|
"\n",
|
||
|
"L'interface se sépare en 2 parties:\n",
|
||
|
"\n",
|
||
|
"![interface replit](./fig/replit_global.png)\n",
|
||
|
"\n",
|
||
|
"- **Zone Script**: Éditeur de texte où l'on pourra écrire notre programme python. Pour executer son contenu, il faudra cliquer sur ![replit run](./fig/replit_run.png)\n",
|
||
|
"- **Zone Shell**: Line de commande pour interagir avec python. Ici vous pouvez tester vos commandes python et presser la touche *entrer* pour l'executer. Par contre on ne peut pas écrire sur plusieurs lignes.\n",
|
||
|
"\n",
|
||
|
"Il est conseillé d'utiliser en priorité la **zone script** car elle permet d'écrire sur plusieurs lignes et de corriger après l'execution. La **zone shell** servira à tester des commandes pour voir directement leurs effets. \n",
|
||
|
"\n",
|
||
|
"Pour sauvergarder votre travail, vous cliquerez sur les 3 points à côté de *Files* puis sur *download as zip*.\n",
|
||
|
"\n",
|
||
|
"![replit save](./fig/replit_files.png)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"--------------------------------------\n",
|
||
|
"## input\n",
|
||
|
"\n",
|
||
|
"Contrairement à ce que l'on a fait hier on ne met plus rien dans les parenthèses d'un `input`.\n",
|
||
|
"\n",
|
||
|
"`input()` est une commande qui va demander à l'utilisateur une information "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Comment t'appelles tu?\n",
|
||
|
"Ivan\n",
|
||
|
"Ah! Tu t'appelles Ivan\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(\"Comment t'appelles tu?\")\n",
|
||
|
"nom = input()\n",
|
||
|
"print(\"Ah! Tu t'appelles\", nom)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Après avoir afficher la question, l'ordinateur se met en pose et attend que l'utilisateur réponde quelque chose. Une fois que l'utilisateur a répondu (a appuyé sur `entrer`) la réponse est stocké, ici, dans la variable `nom`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Condition if\n",
|
||
|
"\n",
|
||
|
"Pour différencier plusieurs cas, on utilisera la commande `if` (si), `elif` (sinon si) et `else` (sinon)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 21,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"a est plus petit que b\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"a = 2\n",
|
||
|
"b = 3\n",
|
||
|
"\n",
|
||
|
"if a > b: # Si\n",
|
||
|
" print(\"a plus grand que b\")\n",
|
||
|
"elif a == b: # Sinon si\n",
|
||
|
" print(\"a est égal à b\")\n",
|
||
|
"else: # Sinon\n",
|
||
|
" print(\"a est plus petit que b\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Boucles for\n",
|
||
|
"\n",
|
||
|
"Les boucles **for** permettent de répéter un certain nombre de fois la même action.\n",
|
||
|
"\n",
|
||
|
"* Générer plusieurs valeurs:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"0\n",
|
||
|
"1\n",
|
||
|
"2\n",
|
||
|
"3\n",
|
||
|
"4\n",
|
||
|
"5\n",
|
||
|
"6\n",
|
||
|
"7\n",
|
||
|
"8\n",
|
||
|
"9\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"for x in range(10):\n",
|
||
|
" print(x)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Python a créé une variable `x` puis lui a fait prendre toutes les valeurs entre 0 et 9.\n",
|
||
|
"\n",
|
||
|
"* Ajouter 5 fois 100 d'affilé"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 22,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Au debut a vaut 2\n",
|
||
|
"a vaut maintenant 102\n",
|
||
|
"a vaut maintenant 202\n",
|
||
|
"a vaut maintenant 302\n",
|
||
|
"a vaut maintenant 402\n",
|
||
|
"a vaut maintenant 502\n",
|
||
|
"Finalement a vaut 502\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"a = 2\n",
|
||
|
"print(\"Au debut a vaut\", a)\n",
|
||
|
"for i in range(5):\n",
|
||
|
" a = a + 100\n",
|
||
|
" print(\"a vaut maintenant\", a)\n",
|
||
|
"print(\"Finalement a vaut\", a)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"* On peut aussi parcourir les lettres d'un mot ou d'une phrase"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 23,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"On enumere >>> boujour toi\n",
|
||
|
"b\n",
|
||
|
"o\n",
|
||
|
"u\n",
|
||
|
"j\n",
|
||
|
"o\n",
|
||
|
"u\n",
|
||
|
"r\n",
|
||
|
" \n",
|
||
|
"t\n",
|
||
|
"o\n",
|
||
|
"i\n",
|
||
|
"Toutes les lettres ont été énumérées\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"phrase = \"boujour toi\"\n",
|
||
|
"print(\"On enumere >>> \", phrase)\n",
|
||
|
"for l in phrase:\n",
|
||
|
" print(l)\n",
|
||
|
"print(\"Toutes les lettres ont été énumérées\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"* Ou encore tous les éléments d'une liste"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 24,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"On enumère >>> ['premier element', 2, 'coucou', 1, 'aie', 0]\n",
|
||
|
"premier element\n",
|
||
|
"2\n",
|
||
|
"coucou\n",
|
||
|
"1\n",
|
||
|
"aie\n",
|
||
|
"0\n",
|
||
|
"Tout a été énuméré\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"liste = [\"premier element\", 2, \"coucou\", 1, \"aie\", 0]\n",
|
||
|
"print(\"On enumère >>>\", liste)\n",
|
||
|
"for i in liste:\n",
|
||
|
" print(i)\n",
|
||
|
"print(\"Tout a été énuméré\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Boucles while\n",
|
||
|
"\n",
|
||
|
"En anglais, `while` signifie **tant que**. On l'utilisera quand on veut refaire quelque plusieurs fois **tant que** quelque chose n'est pas vrais.\n",
|
||
|
"\n",
|
||
|
"Ajouter 3 **tant que** que `u` n'a pas dépassé 19: "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"u vaut 5\n",
|
||
|
"u vaut 8\n",
|
||
|
"u vaut 11\n",
|
||
|
"u vaut 14\n",
|
||
|
"u vaut 17\n",
|
||
|
"u vaut 20\n",
|
||
|
"u a depassé 50 et vaut 20\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"u = 2\n",
|
||
|
"while u < 19:\n",
|
||
|
" u = u + 3\n",
|
||
|
" print(\"u vaut \", u)\n",
|
||
|
"print(\"u a depassé 50 et vaut\", u)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Poser toujours la même question quand que l'utilisateur n'a pas bien répondu:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 26,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Quelle est la couleur du cheval blanc d'Henri 4?\n",
|
||
|
"bleu\n",
|
||
|
"Non! Recommence!\n",
|
||
|
"Quelle est la couleur du cheval blanc d'Henri 4?\n",
|
||
|
"vert\n",
|
||
|
"Non! Recommence!\n",
|
||
|
"Quelle est la couleur du cheval blanc d'Henri 4?\n",
|
||
|
"jaune\n",
|
||
|
"Non! Recommence!\n",
|
||
|
"Quelle est la couleur du cheval blanc d'Henri 4?\n",
|
||
|
"blanc\n",
|
||
|
"Bravo!\n",
|
||
|
"Voila tu as réussi!\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"aPasTrouve = True # True signifie vrai en anglais.\n",
|
||
|
"while aPasTrouve:\n",
|
||
|
" print(\"Quelle est la couleur du cheval blanc d'Henri 4?\")\n",
|
||
|
" rep = input()\n",
|
||
|
" if rep == \"blanc\":\n",
|
||
|
" print(\"Bravo!\")\n",
|
||
|
" aPasTrouve = False # False signifie faux en anglais\n",
|
||
|
" else:\n",
|
||
|
" print(\"Non! Recommence!\")\n",
|
||
|
"print(\"Voila tu as réussi!\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Fonctions\n",
|
||
|
"\n",
|
||
|
"Une fonction est un morceau de programme qui évitera de coder plusieurs fois la même chose. Quand on programme on dit souvent `Si c'est la 3e fois que l'on écrit ces lignes c'est qu'il faut en faire une fonction`.\n",
|
||
|
"\n",
|
||
|
"Voici différentes utilisations de fonctions."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Fonctions numériques\n",
|
||
|
"\n",
|
||
|
"En math, on utilise les fonctions pour décrire les transformations (de nombres le plus souvent). \n",
|
||
|
"\n",
|
||
|
"Par exemple:\n",
|
||
|
"$$ f:x \\mapsto 3x + 2$$\n",
|
||
|
"Cette fonction $f$ prend un nombre, le multiplie par 3 puis ajoute 2. On pourra la coder de la façon suivante:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 27,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def f(x):\n",
|
||
|
" return 3*x+2"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"On vient de coder cette fonction, on peut ensuite l'utiliser"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 28,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"14\n",
|
||
|
"L'image de 2.5 par la fonction f est 9.5\n",
|
||
|
"f(-2) = -4\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(f(4))\n",
|
||
|
"print(\"L'image de 2.5 par la fonction f est \", f(2.5))\n",
|
||
|
"print(\"f(-2) = \", f(-2))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Au collège, on a vu les \"programmes de calculs\". Ils peuvent eux aussi être codé avec des fonctions.\n",
|
||
|
"\n",
|
||
|
"- Choisir un nombre\n",
|
||
|
"- Ajouter 5\n",
|
||
|
"- Diviser par 10\n",
|
||
|
"\n",
|
||
|
"Se code de la façon suivante:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 29,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def programme(nombre):\n",
|
||
|
" reponse = nombre + 5\n",
|
||
|
" reponse = reponse / 10\n",
|
||
|
" return reponse"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 30,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"6.5\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(programme(60))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### D'autres fonctions\n",
|
||
|
"\n",
|
||
|
"Les fonctions numériques sont très utilisées en math mais en programmation, on manipule beaucoup d'autres types de fonctions.\n",
|
||
|
"\n",
|
||
|
"On pourrait vouloir transformer un texte en un titre entouré de symboles `#`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 31,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def entoure(titre):\n",
|
||
|
" return '##### ' + titre + ' #####'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 32,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"##### Mon titre #####\n",
|
||
|
"##### L'histoire du petit chaperon rouge #####\n",
|
||
|
"##### Fin #####\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(entoure('Mon titre'))\n",
|
||
|
"print(entoure(\"L'histoire du petit chaperon rouge\"))\n",
|
||
|
"print(entoure('Fin'))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"On peut même mettre un autre argument (en plus de `titre`) pour entourer avec autre chose que `#`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 33,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def entoure(texte, symbole):\n",
|
||
|
" return symbole*4 + ' ' + texte + ' ' + symbole*4"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 34,
|
||
|
"metadata": {
|
||
|
"scrolled": true
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"---- La belle au bois dormant ----\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(entoure('La belle au bois dormant', '-'))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"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.7.1"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 1
|
||
|
}
|