{
 "metadata": {
  "name": "",
  "signature": "sha256:1630a227c54e735b7c11051ce8769271ee46f9bca6c1916bb26cfdba7c01b546"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Il s\u2019agit de rep\u00e9rer les donn\u00e9es n\u00e9cessaires voire indispensables \u00e0 la\n",
      "r\u00e9solution. Ces donn\u00e9es peuvent \u00eatre:\n",
      "\n",
      "-   num\u00e9riques,\n",
      "-   ou sous forme de textes (on dit souvent cha\u00eenes de caract\u00e8res),\n",
      "-   ou de type logique (\u00e0 deux valeurs possibles, vrai ou faux).\n",
      "\n",
      "Pour affecter une valeur \u00e0 une variable en python, la syntaxe est de la forme:\n",
      "\n",
      "```\n",
      "nom = valeur\n",
      "```\n",
      "\n",
      "<dl>\n",
      "<dt>Affectation</dt>\n",
      "<dd>Dans un programme, une affectation donne une valeur \u00e0 une variable, elle est de la forme <code>v = e</code> avec `v` une variable et `e` une expression.\n",
      "</dd>\n",
      "</dl>\n",
      "\n",
      "Valeurs num\u00e9riques\n",
      "==================\n",
      "\n",
      "Python distingue les entiers(integers), des nombres \u00e0\n",
      "virgule(floating-point) par l'ajout d'une virgule:\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 1\n",
      "b = 1.0\n",
      "\n",
      "print(a, type(a), b, type(b))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1 <class 'int'> 1.0 <class 'float'>\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Mais il impl\u00e9mente aussi des nombres plus exotiques tels que les\n",
      "d\u00e9cimaux, les fractions ou encore les nombres complexes\n",
      "\n",
      "Cha\u00eenes de caract\u00e8res\n",
      "=====================\n",
      "\n",
      "En python3, il existe deux grands types de cha\u00eenes de caract\u00e8res, les\n",
      "strings, et les bytes. Les strings sont simplement entour\u00e9es par des\n",
      "guillemets simples ou doubles.\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 'Mon texte'\n",
      "b = \"Mon deuxi\u00e8me texte\"\n",
      "\n",
      "print(a, type(a), b, type(b))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Mon texte <class 'str'> Mon deuxi\u00e8me texte <class 'str'>\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Les bytes sont pr\u00e9c\u00e9d\u00e9es de la lettre b, il s'agit de texte brut utilis\u00e9\n",
      "par la machine mais pas toujours lisible par un humain.\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = b'Mon texte'\n",
      "print(a, type(a))\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "b'Mon texte' <class 'bytes'>\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "On ne peut repr\u00e9senter simplement que certains caract\u00e8res(les caract\u00e8res\n",
      "ASCII), les caract\u00e8res accentu\u00e9s ne peuvent \u00eatre \u00e9crits directement.\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b = b'Mon deuxi\u00e8me texte'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "SyntaxError",
       "evalue": "bytes can only contain ASCII literal characters. (<ipython-input-5-b0bf0ef2715d>, line 1)",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-5-b0bf0ef2715d>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    b = b'Mon deuxi\u00e8me texte'\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m bytes can only contain ASCII literal characters.\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Un encodage autre que le ASCII doit \u00eatre utilis\u00e9 pour ces caract\u00e8res,\n",
      "comme par exemple le UTF-8 qui est un codage sur huit bits tr\u00e8s r\u00e9pandu.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b = b'Mon deuxi\\xc3\\xa8me texte'\n",
      "print(b)\n",
      "print(b.decode('utf-8'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "b'Mon deuxi\\xc3\\xa8me texte'\n",
        "Mon deuxi\u00e8me texte\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Valeurs logiques\n",
      "================\n",
      "\n",
      "On les appelle les bool\u00e9ens, il ne peuvent avoir que deux valeurs:\n",
      "\n",
      "-   VRAI ou FAUX: True ou False en anglais;\n",
      "-   1 ou 0.\n",
      "\n",
      "On les utilise pour r\u00e9aliser des tests.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "1 < 2, 1 > 2, 2 == 10, 2 >= 1.9, 2 == 2, 2 != 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "(True, False, False, True, True, False)"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "On peut aussi les utiliser pour savoir si une variable existe.\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "bool(a)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "On peut aussi combiner plusieurs tests avec les op\u00e9rateurs bool\u00e9ens `and`, `or` et `not`."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "1 < 2 or 2 < 1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "1 < 2 and 2 < 1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "False"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "not(1 < 2 and 2 < 1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#Listes et dictionnaires\n",
      "\n",
      "Souvent les donn\u00e9es pertinentes doivent \u00eatre agenc\u00e9es sous une forme\n",
      "plus vaste, comme par exemple des listes o\u00f9 on peut ranger de fa\u00e7on ordonn\u00e9e *plusieurs valeurs*.\n",
      "\n",
      "##Listes\n",
      "\n",
      "\n",
      "Les listes sont des collections *ordonn\u00e9es de valeurs*, elles sont\n",
      "entour\u00e9es par des crochets `[]`, leurs \u00e9l\u00e9ments sont s\u00e9par\u00e9s par des\n",
      "virgules.\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = [ 1, 'deux' , 3]\n",
      "type(a)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "list"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "On peut facilement acc\u00e9der \u00e0 la longueur de la liste grace \u00e0 la fonction\n",
      "`len` et \u00e0 chacun de ces \u00e9l\u00e9ments gr\u00e2ce \u00e0 leur index *(Attention le\n",
      "premier \u00e9lement \u00e0 l'index 0)*\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "len(a)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "3"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a[0], a[2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "(1, 3)"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "On peut inversement conna\u00eetre l'indice correspondant \u00e0 une valeur gr\u00e2ce \u00e0 l'attribut `index`."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a.index(1), a.index('deux'), a.index(3)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "(0, 1, 2)"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Dictionnaires\n",
      "\n",
      "Dans un dictionnaire les valeurs de la collection ne sont pas rep\u00e9r\u00e9 par\n",
      "un index, mais par une *cl\u00e9*. Les dictionnaires sont entour\u00e9s d'accolades `{}`.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = { 'nom': 'Doe' , 'prenom': 'John', 'age': 77 }\n",
      "type(a)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "dict"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Pour acc\u00e9der aux \u00e9l\u00e9ments du dictionnaire, il suffit d'appeler la cl\u00e9\n",
      "correspondante, d'autres part la fonction `len` est \u00e9galemnt disponible.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "len(a)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 17,
       "text": [
        "3"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a['nom'], a['age']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 18,
       "text": [
        "('Doe', 77)"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Modification des listes et dictionnaires\n",
      "\n",
      "Les listes et dictionnaires sont des objets **mutables**, c'est \u00e0 dire que l'on peut modifier leur contenu sans cr\u00e9er un nouvel objet. On dit qu'il s'agit de donn\u00e9es [non-persistantes](http://fr.wikipedia.org/wiki/Persistance_%28informatique%29).\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Valeurs initiales\n",
      "liste = [ 1, 'deux' , 3]\n",
      "dict = { 'nom': 'Doe' , 'prenom': 'John', 'age': 77 }\n",
      "print(\"Valeurs initiales:\\n\", liste, dict)\n",
      "\n",
      "# Modification des valeurs par assignation\n",
      "liste[1] = 2\n",
      "dict['age'] = 17\n",
      "\n",
      "print(\"Modification des valeurs par assignation:\\n\", liste, dict)\n",
      "\n",
      "# Ajout d'\u00e9l\u00e9ments\n",
      "liste.append(4)\n",
      "dict['nationalit\u00e9'] = 'fran\u00e7aise'\n",
      "\n",
      "print(\"Ajout d'\u00e9l\u00e9ments:\\n\", liste, dict)\n",
      "\n",
      "# Suppression d'\u00e9l\u00e9ments\n",
      "liste.pop(0)\n",
      "dict.pop('age')\n",
      "\n",
      "print(\"Suppression d'\u00e9l\u00e9ments:\\n\", liste, dict)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Valeurs initiales:\n",
        " [1, 'deux', 3] {'age': 77, 'nom': 'Doe', 'prenom': 'John'}\n",
        "Modification des valeurs par assignation:\n",
        " [1, 2, 3] {'age': 17, 'nom': 'Doe', 'prenom': 'John'}\n",
        "Ajout d'\u00e9l\u00e9ments:\n",
        " [1, 2, 3, 4] {'age': 17, 'nom': 'Doe', 'nationalit\u00e9': 'fran\u00e7aise', 'prenom': 'John'}\n",
        "Suppression d'\u00e9l\u00e9ments:\n",
        " [2, 3, 4] {'nom': 'Doe', 'nationalit\u00e9': 'fran\u00e7aise', 'prenom': 'John'}\n"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Si on a besoin de modifier une liste ou un dictionnaire, mais que l'on veut garder une trace des objets initiaux, il faut commencer par en cr\u00e9er une **copie**, il ne suffit pas de cr\u00e9er une variable supl\u00e9mentaire sans quoi cette variable serait elle aussi modifi\u00e9e si l'objet initial changeait: l'assignation est dite par **r\u00e9f\u00e9rence** dans ce cas."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Valeurs initiales\n",
      "L = [ 1, 'deux' , 3]\n",
      "print(\"Valeurs initiales:\\n\", L)\n",
      "\n",
      "# Cr\u00e9ation d'une r\u00e9f\u00e9rencxe \u00e0 la liste par simple assignation\n",
      "L_ref = L\n",
      "\n",
      "# Cr\u00e9ation d'une copie de la liste\n",
      "L_copie = list(L)\n",
      "\n",
      "# Modification de la liste initiale\n",
      "L[1] = 2\n",
      "\n",
      "print(\"Modification de la liste L:\")\n",
      "print(\"La liste L a bien, \u00e9t\u00e9 modifi\u00e9e:\", L)\n",
      "print(\"La liste L_ref a aussi \u00e9t\u00e9 modifi\u00e9e car il s'agit juste d'une r\u00e9f\u00e9rence vers la liste L:\", L_ref)\n",
      "print(\"La copie L_copie n'a pas \u00e9t\u00e9 modifi\u00e9e:\", L_copie)\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Valeurs initiales:\n",
        " [1, 'deux', 3]\n",
        "Modification de la liste L:\n",
        "La liste L a bien, \u00e9t\u00e9 modifi\u00e9e: [1, 2, 3]\n",
        "La liste L_ref a aussi \u00e9t\u00e9 modifi\u00e9e car il s'agit juste d'une r\u00e9f\u00e9rence vers la liste L: [1, 2, 3]\n",
        "La copie L_copie n'a pas \u00e9t\u00e9 modifi\u00e9e: [1, 'deux', 3]\n"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Entr\u00e9e des donn\u00e9es\n",
      "==================\n",
      "\n",
      "Dans cette phase peut aussi figurer ce qu\u2019on appelle l\u2019entr\u00e9e des\n",
      "donn\u00e9es, qui peut se manifester par la saisie de caract\u00e8res ou de\n",
      "nombres sur le clavier, ou la lecture de la position du pointeur de la\n",
      "souris, ou encore par la lecture d\u2019un fichier contenant ces nombres ou\n",
      "caract\u00e8res.\n",
      "\n",
      "Il s\u2019agit aussi de rep\u00e9rer les r\u00e9sultats interm\u00e9diaires qu\u2019il est bon de\n",
      "m\u00e9moriser pour la suite car indispensables au traitement.\n",
      "\n",
      "Il est parfois utile d\u2019utiliser des variables auxiliaires pour ne pas\n",
      "perturber les donn\u00e9es initiales.\n",
      "\n",
      "Entr\u00e9e des donn\u00e9es au clavier\n",
      "-----------------------------\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "chiffre = input(\"Entrer la valeur du chiffre souhait\u00e9: \")\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Entrer la valeur du chiffre souhait\u00e9: 7\n"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(chiffre)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Attention cependant, cette valeur est une cha\u00eene de caract\u00e8re, et les op\u00e9rations sont celles des `string`s."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(type(chiffre))\n",
      "chiffre*10"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<class 'str'>\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 23,
       "text": [
        "'7777777777'"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Il convient de la convertir un nombre entier ou flottant avant d'utiliser la valeur."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "chiffre = int(chiffre)\n",
      "10*chiffre"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 24,
       "text": [
        "70"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Lecture d'un fichier\n",
      "--------------------\n",
      "\n",
      "Voici par exemple comment ouvrir et lire un fichier appel\u00e9 `lorem.txt`\n",
      "contenu dans le r\u00e9p\u00e9rtoire `data` du dossier courant.\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fichier = open('data/lorem.txt')\n",
      "fichier.read()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\\nvero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,\\nno sea takimata sanctus est Lorem ipsum dolor sit amet.\\n'"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Dans la sortie pr\u00e9c\u00e9dente, les caract\u00e8res `\\n` repr\u00e9sentent les sauts de\n",
      "ligne, on peut aussi lire le fichier ligne par ligne."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fichier = open('data/lorem.txt')\n",
      "fichier.readline()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 29,
       "text": [
        "'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\\n'"
       ]
      }
     ],
     "prompt_number": 29
    }
   ],
   "metadata": {}
  }
 ]
}
