Skip to content
Snippets Groups Projects
1_Instructions_de_base.ipynb 9.27 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    {
     "cells": [
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "...on parlait de **[programmation impérative](https://fr.wikipedia.org/wiki/Programmation_imp%C3%A9rative) [structurée](https://fr.wikipedia.org/wiki/Programmation_structur%C3%A9e)**..."
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "# Les instructions de base"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Instructions d'assignation"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 1,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "5\n"
         ]
        }
       ],
       "source": [
        "a = 3\n",
        "b = 2\n",
        "print(a+b)"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Pour connaître les variables qui sont définies dans le _scope_ actuel :"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 2,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/plain": [
           "['In',\n",
           " 'Out',\n",
           " '_',\n",
           " '__',\n",
           " '___',\n",
           " '__builtin__',\n",
           " '__builtins__',\n",
           " '__doc__',\n",
           " '__loader__',\n",
           " '__name__',\n",
           " '__package__',\n",
           " '__spec__',\n",
           " '_dh',\n",
           " '_i',\n",
           " '_i1',\n",
           " '_i2',\n",
           " '_ih',\n",
           " '_ii',\n",
           " '_iii',\n",
           " '_oh',\n",
           " 'a',\n",
           " 'b',\n",
           " 'exit',\n",
           " 'get_ipython',\n",
           " 'quit']"
          ]
         },
         "execution_count": 2,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "dir()"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Pour effacer une variable :"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 3,
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
       "metadata": {},
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
       "outputs": [],
       "source": [
        "z = 2\n",
        "del(z)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 4,
       "metadata": {},
       "outputs": [
        {
         "ename": "NameError",
         "evalue": "name 'z' is not defined",
         "output_type": "error",
         "traceback": [
          "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
          "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          "\u001b[0;32m<ipython-input-4-3a710d2a84f8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          "\u001b[0;31mNameError\u001b[0m: name 'z' is not defined"
         ]
        }
       ],
       "source": [
        "z"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Instructions conditionnelles"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 5,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "3\n"
         ]
        }
       ],
       "source": [
        "if a > b:\n",
        "    print(a)\n",
        "else:\n",
        "    print(b)"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Indentation : espaces ou tabulation ?\n",
        "\"Use 4 spaces per indentation level.\", cf. https://www.python.org/dev/peps/pep-0008/"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 6,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "La valeur de la variable a est supérieure à 1.\n"
         ]
        }
       ],
       "source": [
        "if a > 1:\n",
        "    print('La valeur de la variable a est supérieure à 1.')\n",
        "elif a < 1:\n",
        "    print('La valeur de la variable a est inférieure à 1.')\n",
        "else:\n",
        "    print('La valeur de la variable a est égale à 1.')"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Instructions de bouclage"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 7,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "1\n",
          "2\n",
          "3\n",
          "4\n",
          "5\n"
         ]
        }
       ],
       "source": [
        "a = 0\n",
        "while a < 5:\n",
        "    a += 1\n",
        "    print(a)"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "N.B. : Il n'y a pas de `do...while`, mais l'on peut l'emuler"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 8,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "1\n",
          "2\n",
          "3\n",
          "4\n",
          "5\n"
         ]
        }
       ],
       "source": [
        "a = 0\n",
        "while True:\n",
        "    a += 1\n",
        "    print(a)\n",
        "    if a >= 5:\n",
        "        break"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 9,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "0\n",
          "1\n",
          "2\n"
         ]
        }
       ],
       "source": [
        "for i in range(3):\n",
        "    print(i)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 10,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "0\n",
          "1\n",
          "2\n"
         ]
        }
       ],
       "source": [
        "for i in range(0, 3):\n",
        "    print(i)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 11,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "1\n",
          "2\n"
         ]
        }
       ],
       "source": [
        "for i in range(1, 3):\n",
        "    print(i)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 12,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "Help on class range in module builtins:\n",
          "\n",
          "class range(object)\n",
          " |  range(stop) -> range object\n",
          " |  range(start, stop[, step]) -> range object\n",
          " |  \n",
          " |  Return an object that produces a sequence of integers from start (inclusive)\n",
          " |  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.\n",
          " |  start defaults to 0, and stop is omitted!  range(4) produces 0, 1, 2, 3.\n",
          " |  These are exactly the valid indices for a list of 4 elements.\n",
          " |  When step is given, it specifies the increment (or decrement).\n",
          " |  \n",
          " |  Methods defined here:\n",
          " |  \n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |  __bool__(self, /)\n",
          " |      self != 0\n",
          " |  \n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |  __contains__(self, key, /)\n",
          " |      Return key in self.\n",
          " |  \n",
          " |  __eq__(self, value, /)\n",
          " |      Return self==value.\n",
          " |  \n",
          " |  __ge__(self, value, /)\n",
          " |      Return self>=value.\n",
          " |  \n",
          " |  __getattribute__(self, name, /)\n",
          " |      Return getattr(self, name).\n",
          " |  \n",
          " |  __getitem__(self, key, /)\n",
          " |      Return self[key].\n",
          " |  \n",
          " |  __gt__(self, value, /)\n",
          " |      Return self>value.\n",
          " |  \n",
          " |  __hash__(self, /)\n",
          " |      Return hash(self).\n",
          " |  \n",
          " |  __iter__(self, /)\n",
          " |      Implement iter(self).\n",
          " |  \n",
          " |  __le__(self, value, /)\n",
          " |      Return self<=value.\n",
          " |  \n",
          " |  __len__(self, /)\n",
          " |      Return len(self).\n",
          " |  \n",
          " |  __lt__(self, value, /)\n",
          " |      Return self<value.\n",
          " |  \n",
          " |  __ne__(self, value, /)\n",
          " |      Return self!=value.\n",
          " |  \n",
          " |  __reduce__(...)\n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |      Helper for pickle.\n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |  \n",
          " |  __repr__(self, /)\n",
          " |      Return repr(self).\n",
          " |  \n",
          " |  __reversed__(...)\n",
          " |      Return a reverse iterator.\n",
          " |  \n",
          " |  count(...)\n",
          " |      rangeobject.count(value) -> integer -- return number of occurrences of value\n",
          " |  \n",
          " |  index(...)\n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |      rangeobject.index(value) -> integer -- return index of value.\n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |      Raise ValueError if the value is not present.\n",
          " |  \n",
          " |  ----------------------------------------------------------------------\n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |  Static methods defined here:\n",
          " |  \n",
          " |  __new__(*args, **kwargs) from builtins.type\n",
          " |      Create and return a new object.  See help(type) for accurate signature.\n",
          " |  \n",
          " |  ----------------------------------------------------------------------\n",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
          " |  Data descriptors defined here:\n",
          " |  \n",
          " |  start\n",
          " |  \n",
          " |  step\n",
          " |  \n",
          " |  stop\n",
          "\n"
         ]
        }
       ],
       "source": [
        "help(range)"
       ]
      }
     ],
     "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",
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
       "version": "3.7.5"
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }