{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "[prev: Exécuter son script](command-line.ipynb) | [home](../index.ipynb) | [next: Introduction aux fonctions](fonction-intro.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Noms et variables\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Utiliser des variables pour être plus flexible" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "La même instruction, `print(a * 2)` , donne différents résultats selon la valeur de `a`.\n", "\n", "-> séparation entre opérations et données" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "toto\n", "tata\n", "42\n" ] } ], "source": [ "a = 'to'\n", "print(a * 2)\n", "\n", "a = 'ta'\n", "print(a * 2)\n", "\n", "a = 21\n", "print(a * 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Un même nom peut être utilisé successivement pour différentes variables/valeurs sans contrainte de type." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Utiliser un nom inconnu est difficile" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "NameError", "evalue": "name 'inconnu' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43minconnu\u001b[49m\n", "\u001b[1;31mNameError\u001b[0m: name 'inconnu' is not defined" ] } ], "source": [ "inconnu" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'XXX'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inconnu = 'XXX'\n", "inconnu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Qu'est-ce qu'un nom licite ?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Il utilise seulement les caractères suivants\n", "\n", "- underscore : `_`\n", "- lettres minuscules : `[a-z]`\n", "- lettres majuscules : `[A-Z]`\n", "- nombres : `[0-9]`\n", "\n", "**MAIS** il ne commence pas par un nombre (`[0-9]`)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# exemples qui marchent\n", "\n", "x = 2\n", "\n", "camion = 'vroum'\n", "\n", "HAUT = 'O'\n", "\n", "unnomlongmaispaslisible = 0\n", "\n", "un_nom_long_lisible = 1\n", "\n", "UnNomLongAussiLisible = 2\n", "\n", "_avec_une_patte_devant = 3\n", "\n", "__avec_des_longues_pattes_partout__ = 4" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (1202299578.py, line 2)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[5], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m allo? = 3\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "# exemple qui marche pas\n", "allo? = 3" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "cannot assign to expression here. Maybe you meant '==' instead of '='? (4131215381.py, line 2)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[6], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m deux-nom = 3\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot assign to expression here. Maybe you meant '==' instead of '='?\n" ] } ], "source": [ "# exemple qui marche pas\n", "deux-nom = 3" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid decimal literal (1058342908.py, line 2)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[7], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m 0_commence_avec_zero = False\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid decimal literal\n" ] } ], "source": [ "# exemple qui marche pas\n", "0_commence_avec_zero = False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**De plus**, Il ne doit pas être déjà réservé par le langage" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Voici les 35 mots-clefs de Python:\n", "\n", "- False - None - True - and - as - assert \n", "- async - await - break - class - continue - def \n", "- del - elif - else - except - finally - for \n", "- from - global - if - import - in - is \n", "- lambda - nonlocal - not - or - pass - raise \n", "- return - try - while - with - yield \n" ] } ], "source": [ "from keyword import kwlist\n", "\n", "width = 6\n", "\n", "print(f'Voici les {len(kwlist)} mots-clefs de Python:\\n')\n", "\n", "for i in range(0, len(kwlist), width):\n", " for word in kwlist[i:i + width]:\n", " print('-', word.ljust(12), end=' ')\n", " print()\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (986342119.py, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[9], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m lambda = 123\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "lambda = 123" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "tags": [] }, "outputs": [], "source": [ "lambda_ = 123" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extra" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] }, { "ename": "TypeError", "evalue": "'str' object is not callable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[11], line 4\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mint\u001b[39m(\u001b[38;5;241m3.14\u001b[39m))\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124moups\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m----> 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m3.14\u001b[39;49m\u001b[43m)\u001b[49m)\n", "\u001b[1;31mTypeError\u001b[0m: 'str' object is not callable" ] } ], "source": [ "# Attention : 'int', 'float', 'str' ne sont pas réservés !\n", "print(int(3.14))\n", "int = 'oups'\n", "print(int(3.14))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "int = type(1) # un moyen de restaurer 'int'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercice" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jouer un peu avec les opérateurs `+`, `*` ...\n", "\n", "Essayer un exemple concret avec la conversion de température °C/°F [de formule $ Celsius = \\frac{5}{9}(Fahrenheit - 32) $](https://fr.wikipedia.org/wiki/Degr%C3%A9_Fahrenheit).\n", "\n", "Expérimenter l'utilisation du `print(nom)` pour voir le résultat de quelques calculs." ] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 4 }