{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Course 1: Python as a powerful calculator\n",
    "\n",
    "This is the first of three worksheets to learn the [Python programming language](https://www.python.org/). In this one we will learn\n",
    "\n",
    "- how to manipulate a Jupyter worksheet\n",
    "- some basic objects (of type `bool`, `int`, `float`, `str`, `type`)\n",
    "- some binary operators (`+`, `-`, `*`, `/`, `//`, `**`, `%`)\n",
    "- comparison operators (`==`, `!=`, `<`, `<=`, `>`, `>=`)\n",
    "- the functions `chr`, `isinstance`, `len`, `ord` and `print`\n",
    "- variables and assignment (`=`)\n",
    "- calling functions and methods\n",
    "- using Python libraries (here [`math`](https://docs.python.org/3/library/math.html))\n",
    "- making simple graphics using [`numpy`](https://numpy.org/) and [`matplotlib`](https://matplotlib.org/)\n",
    "\n",
    "The [Jupyter notebook](http://jupyter.org/) is a way to program inside a web browser (like Firefox, Chrome or Safari). It does not require an internet connection. It is very convenient to make exercises but also for making presentations. We will see other way of using Python later.\n",
    "\n",
    "A notebook is made of cells. Each cell contains either text or code. To execute a cell you need to press `Shift-Enter`. To change the cell type you need to go in the menu (top of the page \"Cell -> Cell Type\").\n",
    "\n",
    "Start by executing the cells below, one at a time. Try to understand what it is happening."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1 + 3) * 2 - 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "114313414930249341432222123134123"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# this is a comment\n",
    "114313414930249341432222123134123"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1.23\n",
    "1.2e10\n",
    "float(12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.23\n",
      "12000000000.0\n",
      "12.0\n"
     ]
    }
   ],
   "source": [
    "print(1.23)\n",
    "print(1.2e10)\n",
    "print(float(12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 20000000000.0 False\n"
     ]
    }
   ],
   "source": [
    "print(1, 2e10, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I am a string! Hello!'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"I am a string! Hello!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am a string! Hello!\n"
     ]
    }
   ],
   "source": [
    "print(\"I am a string! Hello!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variables\n",
    "---------\n",
    "\n",
    "A variable in computer science is a different concept from the one in mathematics. A computer science variable is a name that contains a value (a kind of *\"named box\"*). Whereas a mathematical variable is an unknown value.\n",
    "\n",
    "To initialize a variable, one uses the syntax `name = value` as in\n",
    "```python\n",
    "a = 3\n",
    "```\n",
    "This operations is called *assignment* and is very different from mathematical equality.\n",
    "\n",
    "Once a variable is assigned you can manipulate it, like printing\n",
    "```python\n",
    "print(a)\n",
    "```\n",
    "\n",
    "**Exercise:** Copy (`Ctrl-c`), paste (`Ctrl-v`) and execute (`Shift-Enter`) the above two commands in the code cells below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** What will be the output of\n",
    "```python\n",
    "a = 1\n",
    "a = a + 2\n",
    "a = 3 * a\n",
    "print(a)\n",
    "```\n",
    "Check your answer by copying (`Ctrl-c`), pasting (`Ctrl-v`) and executing (`Shift-Enter`) the code in a cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** What will be the output of\n",
    "```python\n",
    "a1 = 12\n",
    "other_variable = -5\n",
    "a1 = other_variable\n",
    "a1 = 3\n",
    "print(other_variable)\n",
    "```\n",
    "Check your answer by copying (`Ctrl-c`), pasting (`Ctrl-v`) and executing (`Shift-Enter`) the code in a cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Types\n",
    "-----\n",
    "\n",
    "Each Python object has a *type*. Namely an integer is of type \"`int`\" and a floating point has type \"`float`\".  A type defines roughly how an object is represented in the memory of the computer and what kind of functions can be applied to it. To get the type of a variable you can use \"`type`\" as shown below.\n",
    "\n",
    "To check that a given value has a given type, there is the function `isinstance`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = False\n",
    "print(\"v =\", v, \"has type\", type(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = 1\n",
    "print(\"v =\", v, \"has type\", type(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = 1.2\n",
    "print(\"v =\", v, \"has type\", type(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = \"hey\"\n",
    "print(\"v =\", v, \"has type\", type(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = bool\n",
    "print(\"v =\", v, \"has type\", type(v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "isinstance(1, int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "isinstance(1.5, float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "isinstance(1.5, str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**\n",
    "\n",
    "Guess what is the output of each of the command below. Check your answer by executing each statement in code cells (to create new code cell you can use the menu \"Insert -> Insert Cell Below\").\n",
    "\n",
    "```python\n",
    "type(12)\n",
    "type(1.34)\n",
    "type(\"ha ha\")\n",
    "type(False)\n",
    "type(int)\n",
    "type(print)\n",
    "isinstance(True, bool)\n",
    "isinstance(13, bool)\n",
    "isinstance(1, float)\n",
    "isinstance(1.5, int)\n",
    "isinstance(1.0, int)    \n",
    "isinstance(True, int)\n",
    "isinstance(int, int)\n",
    "isinstance(bool, type)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Types can be used for making explicit conversions\n",
    "```python\n",
    "s = \"1423331940\"\n",
    "a = int(s)       # the integer whose digits are given by the string s\n",
    "t = str(a * a)   # the digits of the square of the integer a\n",
    "u = int(t + t)   # what is this?\n",
    "```\n",
    "**Exercise:**\n",
    "- Copy, paste and execute the above code\n",
    "- Use `print` to see the values of `s`, `a`, `t` and `u`\n",
    "- Use `type` to get their types\n",
    "- What does the operation `+` on strings?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** If you have a Python floating point number `a` what is the result of `int(a)`? (you need to guess by doing examples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Binary operations (opérations binaires)\n",
    "---------------------------------------\n",
    "\n",
    "Usual arithmetic operations (like sum, multiplication, division, etc) can be done using the following symbols\n",
    "\n",
    "| operator | name               |\n",
    "|----------|--------------------|\n",
    "| `+`      | addition           |\n",
    "| `*`      | multiplication     |\n",
    "| `/`      |(true) division     |\n",
    "| `//`     | (floor) division   |\n",
    "| `**`     | power              |\n",
    "| `%`      | modulo (remainder) |\n",
    "\n",
    "We already have encountered some of them.\n",
    "\n",
    "**Exercise:**\n",
    "- What is the value of $123582459365376458635 + 25437453754812913859284$?\n",
    "- What is the remainder of the Euclidean division of $5^{10}$ by $3^{10}$?\n",
    "- Use the function `len` (that computes the length of an object) to get the number of digits of $2^{100}$ (in base $10$)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Not all operations are available for any type, check the output of\n",
    "```python\n",
    "\"123\" * \"hey\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And it is also sometimes possible to have different types on both side of an operation\n",
    "```python\n",
    "1 + 2.0      # integer + float\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You will learn the meaning of operations on some basic types in the following exercise.\n",
    "\n",
    "**Exercise:**\n",
    "\n",
    "For each of the operation below guess what is the output. Then check your answer by executing the code.\n",
    "\n",
    "```python\n",
    "1 + 1.0\n",
    "1.0 + 1\n",
    "1 * 1.0\n",
    "3 - 5\n",
    "type(12 / 3)\n",
    "type(12 // 3)\n",
    "1 / 0\n",
    "3**1000\n",
    "3**1000.0\n",
    "3.0**1000\n",
    "\"haha\" + \" ho\"\n",
    "\"haha\" * \"ho\"\n",
    "\"hey!\" * 10\n",
    "True + False\n",
    "True + True\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**\n",
    "- Can you add/mutiply/subtract/divide an object of type `int` and an object of type `bool`?\n",
    "- What kind of operations are these?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparisons\n",
    "-----------\n",
    "\n",
    "To compare two values there are the following operators\n",
    "\n",
    "| operator | name            |\n",
    "|----------|-----------------|\n",
    "| `==`     | equal           |\n",
    "| `!=`     | different       |\n",
    "| `<`      | lesser          |\n",
    "| `<=`     | lesser or equal |\n",
    "| `>`      | greater         |\n",
    "| `>=`     | greater or equal|\n",
    "\n",
    "The result of a comparison is a boolean (type `bool`). You have two examples below, execute them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1 + 1 == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "5 * (12 - 7) == 26"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**\n",
    "\n",
    "For each of the following command guess what is the output and then check your answers.\n",
    "```python\n",
    "3 > 12\n",
    "1 == 1.0\n",
    "1 == \"1\"\n",
    "\"kigali\" < \"rwanda\"\n",
    "\"a\" >= \"aa\"\n",
    "\"a\" < \"A\"\n",
    "1 == 1e60 * 1 / (1e60 + 1)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Comparison is another kind of binary operation. In particular its result can be affected to a variable for doing further computations. For example\n",
    "```python\n",
    "a = 3\n",
    "b = 5\n",
    "c = 3 * a < 2 * b\n",
    "d = 5 * a < 3 * b\n",
    "print(c == d)\n",
    "```\n",
    "\n",
    "**Exercise:** Guess what is the output of the above code. Check your answer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** For a Python integer `a`, do we always have `int(float(a)) == a`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**\n",
    "\n",
    "- Set three variables `a`, `b` and `c` to be respectively the floating point numbers `1.5e100`, `0.1` and `-1.5e100`\n",
    "- Compute `a + b + c` and `a + c + b`. What do you see? Could you explain why?\n",
    "- Can you reproduce such behavior with integer variables?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Playing with strings (jouons avec les chaînes de caractères)\n",
    "------------------------------------------------------------\n",
    "\n",
    "Each character of a string is encoded by a number. For example \"K\" -> 75, \"e\" -> 101, \"n\" -> 110, \"y\" -> 121, \"a\" -> 97. This encoding is called [ASCII](https://en.wikipedia.org/wiki/ASCII). There are two Python functions to make the translation between character and integers\n",
    "- `ord`: from character to its ASCII code\n",
    "- `chr`: from an integer to its associated character\n",
    "\n",
    "The code ASCII is the code used when comparing strings.\n",
    "\n",
    "**Exercise:**\n",
    "- What are the ASCII codes of `\"A\"` and `\"a\"`?\n",
    "- Using the answer to the previous question, guess what is the result of `\"a\" < \"A\"`? Check in a code cell."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are a lot of useful operations available on strings. Contrarily to all operations we have seen until now these are *methods*. That is, instead of writing `f(x)` we write `x.f()`. A method is somehow a function attached to an object. This concept of method comes from [Object Oriented Programming](https://en.wikipedia.org/wiki/Object-oriented_programming) (ou [Programmation Orientée Objet](https://fr.wikipedia.org/wiki/Programmation_orient%C3%A9e_objet) en français)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = \"My name is Victor\"\n",
    "print(s1)                            #  print is a function\n",
    "s2 = s1.replace(\"Victor\", \"Sonia\")   # replace is a method\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**IMPORTANT** You can get the list of all methods of a given object by writing the name of the object with a dot and pressing the `<TAB>` key. This is called *tab-completion* and is very useful to retrieve information about the objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise**:\n",
    "- Using tab-completion, find the name of the method to put all letters of a string in capitals\n",
    "- Use it for the string `s1` defined above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise**:\n",
    "We define the sequence of strings $s_0$, $s_1$, $s_2$, ... as follows:\n",
    "\n",
    "* $s_0 = a$\n",
    "* $s_{2n+1}$ is obtained from $s_{2n}$ by replacing $a$ with $ab$\n",
    "* $s_{2n}$ is obtained from $s_{2n-1}$ by replacing $b$ with $ba$\n",
    "\n",
    "For example: $s_1 = ab$ and $s_2 = $aba$. In this exercise, you are asked to study the above sequence.\n",
    "\n",
    "1. Set the variables `s0`, `s1`, `s2`, ... `s8` to the first terms of the sequence using the method `replace`.\n",
    "\n",
    "2. Print each element of the sequence you defined together with their length\n",
    "\n",
    "3. Now for each of them print the number of `\"a\"` and the number of `\"b\"` they contain (*hint*: use tab-completion to find the name of the method)\n",
    "\n",
    "4. Did you recognize these numbers?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The library `math`\n",
    "------------------\n",
    "\n",
    "A Python library is a set of features that are not loaded on startup. Python contains itself around 50 libraries (a complete list is available at https://docs.python.org/3/library/) and there are thousands of them available.\n",
    "\n",
    "To use a library one needs to use the `import` command (like you did with the `turtle`)\n",
    "```python\n",
    "from this_module import this_function\n",
    "this_function(18)\n",
    "```    \n",
    "or\n",
    "```python\n",
    "import this_module\n",
    "this_module.this_function(18)\n",
    "```\n",
    "\n",
    "The advantage of the first method is that names are shorter. The advantage of the second is that there are no ambiguity (e.g. there is a function `cos` in the modules `math`, `cmath` and `numpy`).\n",
    "\n",
    "In the cells below we show how to import and use the [math](https://docs.python.org/3/library/math.html) library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "math.acos(0.07)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "math.pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "math.cos(math.pi / 4) == math.sqrt(2) / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "**Exercise:** gives an approximate value of `log(2)` and `exp(2)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** What is the number of digits of $2^{10000000000}$ (written in base $10$)?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Graphics with `numpy` and `matplotlib`\n",
    "--------------------------------------\n",
    "\n",
    "We will now learn about basic plotting. There are *many* Python modules available with plotting. We will consider `matplotlib` which is popular and well developed (you can have a look at http://matplotlib.org/). The module `matplotlib` uses in the background the module `numpy` (http://www.numpy.org/). We will use only one command of `matplotlib` named `plot`.\n",
    "\n",
    "You first need to load the module `matplotlib`. In a first cell copy paste and execute the following command\n",
    "\n",
    "    import matplotlib.pyplot as plt\n",
    "    \n",
    "This command makes available the module `matplotlib.pyplot` using the shortcut `plt`. In a second cell copy, paste and execute the following commands\n",
    "\n",
    "```python\n",
    "X = [0, 1, 2, 1]     # a list of 4 numbers\n",
    "Y = [0, 1, -1, 0]    # another list of 4 numbers\n",
    "plt.plot(X, Y)\n",
    "plt.show()\n",
    "```\n",
    "The command `plot` is quite simple: you just need to provide the list of $x$-coordinates and $y$-coordinates. Optionally you can provide arguments to change the linestyle or color. For example the following code will make a fancier line\n",
    "\n",
    "```python\n",
    "X = [0, 1, 2, 1]\n",
    "Y = [0, 1, -1, 0]\n",
    "plt.plot(X, Y, '-Dm')\n",
    "plt.show()\n",
    "```\n",
    "**IMPORTANT:** You can get all available options by consulting the documentation with `plt.plot?`.\n",
    "\n",
    "You can superpose various plot on the same figure by calling several times `plot` as in\n",
    "```python\n",
    "plt.plot([0, 1, 2], [-1, 1, -1], '.r')\n",
    "plt.plot([-1, 0, 1, 2, 3], [-2, 2, -2, 2, -2], 'y')\n",
    "plt.show()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to make more complex plots we will use a command to create large list of numbers. This function is part of `numpy` that will be also used next week. The function is the following\n",
    "```python\n",
    "numpy.arange(start, stop, step)\n",
    "```\n",
    "It creates an array of real floating point numbers from `start` to `stop` where the difference between each term is `step`. To use it you first need to import the module, for example with\n",
    "```python\n",
    "import numpy as np\n",
    "```\n",
    "and then the function can be used as\n",
    "```python\n",
    "np.arange(0, 2 * math.pi, 0.1)\n",
    "```\n",
    "The Python module `numpy` contains also most mathematical functions that exists in `math` and `cmath`. The difference is that in `numpy` these functions operate on array (component wise). Copy paste and execute the following example. What do you see?\n",
    "```python\n",
    "X = np.arange(0, 2 * math.pi, 0.1)\n",
    "Y = np.cos(X)\n",
    "plt.plot(X, Y)\n",
    "plt.show()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**\n",
    "- Draw a regular pentagon (5 vertices) and a regular octagon (8 vertices).\n",
    "- Draw the [regular star-polygons](https://en.wikipedia.org/wiki/Star_polygon) {7/2}, {7/3} and {7/4}."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:**\n",
    "\n",
    "- Using `numpy.arange` and `matplotlib.pyplot.plot` draw a circle with center $(0,0)$ and radius 1\n",
    "- Make a picture of the ellipse $E_1$ with equation $$ x^2 + 2 y^2 = 1.$$\n",
    "- Make a picture of the ellipse $E_2$ with equation $$ x^2 + 2xy + 2y^2 = 1$$\n",
    "- Make a last picture with all figures: the circle in red, the first ellipse in yellow and the second ellipse in green\n",
    "- Add a legend to the picture (*hint: have a look at the documentation of `plt.plot?`*)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (C) 2016 Vincent Delecroix <vincent.delecroix@u-bordeaux.fr>\n",
    "\n",
    "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0\n",
    "International License (CC BY-NC-SA 4.0). You can either read the\n",
    "[Creative Commons Deed](https://creativecommons.org/licenses/by-nc-sa/4.0/)\n",
    "(Summary) or the [Legal Code](https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode)\n",
    "(Full licence)."
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}