{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Calculatrice et arithm\u00e9tique avec Python\n", "\n", "Dans cette section, nous assumons que la version de Python est la version 3:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from __future__ import division, print_function" ], "outputs": [], "metadata": {} }, { "source": [ "## Op\u00e9rations de base en Python\n", "\n", "Les op\u00e9rations de base (addition, soustraction, multiplication, division) sur les nombres se font avec les op\u00e9rateurs +, -, * et / :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "13.14 + 1.2" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "14.34" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "14.34 - 1.2" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "13.14" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "6 * 9" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "54" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "54 / 9" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "6.0" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La division d'un nombre par z\u00e9ro retourne une erreur:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "53 / 0" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "Traceback (most recent call last):\n", "...\n", "ZeroDivisionError: division by zero" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Exposant\n", "\n", "Le calcul d'une puissance se fait avec la double ast\u00e9risque ** :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "2 ** 8" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "256" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "**ATTENTION**\n", "\n", "En Python, l'op\u00e9rateur ^ ne calcule pas l'exposant, mais fait plut\u00f4t une op\u00e9ration sur la repr\u00e9sentation binaire des nombres entiers (ou exclusif bit \u00e0 bit):" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "5 ^ 3" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "6" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Racine n-i\u00e8me\n", "\n", "Le calcul d'une puissance permet aussi de calculer la racine n-i\u00e8me d'un nombre:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "256 ** (1 / 8)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "2.0" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Reste et quotient de la division\n", "\n", "Le *reste* de la division d'un nombre entier par un autre se fait avec le symbole %. Par exemple, on calcule le reste de la division du nombre 94 par 10:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "94 % 10" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "L'op\u00e9ration a // b lorsque a et b sont des nombres entiers retourne le *quotient* de la division a par b :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "94 // 10" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "9" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Fonctions et constantes math\u00e9matiques en Python\n", "\n", "Le module math de Python contient un certain nombre de fonctions et constantes math\u00e9matiques que l'on retrouve sur une calculatrice:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "acos atanh e factorial hypot log10 sin\n", "acosh ceil erf floor isinf log1p sinh\n", "asin copysign erfc fmod isnan modf sqrt\n", "asinh cos exp frexp ldexp pi tan\n", "atan cosh expm1 fsum lgamma pow tanh\n", "atan2 degrees fabs gamma log radians trunc" ], "outputs": [], "metadata": {} }, { "source": [ "On trouvera leur documentation sur \n", "\n", "Pour importer quelquechose de ce module, il faut d'abord l'importer avec la syntaxe from math import quelquechose. Par exemple, pour importer la fonction factorial, on proc\u00e8de de la fa\u00e7on suivante:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import factorial\n", "factorial(4)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "24" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Alternativement, on peut aussi importer le module math et proc\u00e9der ainsi:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "import math\n", "math.factorial(4)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "24" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "De m\u00eame pour les fonctions trigonom\u00e9triques, on les importe de la fa\u00e7on suivante:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import sin, cos, tan, pi" ], "outputs": [], "metadata": {} }, { "source": [ "Pour importer toutes les fonctions d'un module, il suffit d'\u00e9cire from module import *. Par exemple, pour importer toutes les fonctions du module math, on \u00e9crit:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import *" ], "outputs": [], "metadata": {} }, { "source": [ "On v\u00e9rifie que le sinus d'un angle de 90 degr\u00e9s est bien \u00e9gal \u00e0 1:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "sin(90)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "0.8939966636005579" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Oups, l'argument doit \u00eatre \u00e9crit est en radians (90 degr\u00e9s est \u00e9gal \u00e0 $\\pi/2$ radians) et on obtient bien 1:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "sin(pi/2)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1.0" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La constante $\\pi$ du module math retourne une valeur approch\u00e9e \u00e0 une quinzaine de d\u00e9cimales:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "pi" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "3.141592653589793" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Les fonctions degrees et radians permettent de passer d'une unit\u00e9 d'angle \u00e0 l'autre:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import degrees, radians\n", "degrees(pi)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "180.0" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "radians(180)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "3.141592653589793" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Extraction de la racine carr\u00e9e avec la fonction sqrt :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import sqrt\n", "sqrt(100)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "10.0" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Calcul des racines du polyn\u00f4me $3x^2 + 7x + 2$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import sqrt\n", "(- 7 + sqrt(7**2 - 4 * 3 * 2) ) / (2 * 3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-0.3333333333333333" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "(- 7 - sqrt(7**2 - 4 * 3 * 2) ) / (2 * 3)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "-2.0" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Acc\u00e9der \u00e0 la documentation d'une fonction\n", "\n", "En Python, pour obtenir de l'information sur une fonction, on peut \u00e9crire help(fonction). Par exemple, si on ne sait pas \u00e0 quoi peut bien servir la fonction hypot :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "from math import hypot\n", "help(hypot)" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "Help on built-in function hypot in module math:\n", "hypot(...)\n", " hypot(x, y)\n", " Return the Euclidean distance, sqrt(x*x + y*y)." ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "En IPython, on peut consulter la documentation d'une fonction en ajoutant un point d'interrogation avant ou apr\u00e8s le nom de la fonction. Cela fonctionne aussi dans l'interface Jupiter, ce qui ouvre une fen\u00eatre au bas de la page:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "?hypot" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "Docstring:\n", "hypot(x, y)\n", "Return the Euclidean distance, sqrt(x*x + y*y).\n", "Type: builtin_function_or_method\n" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "hypot?" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "Docstring:\n", "hypot(x, y)\n", "Return the Euclidean distance, sqrt(x*x + y*y).\n", "Type: builtin_function_or_method" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Parenth\u00e8ses et priorit\u00e9 des op\u00e9rations\n", "\n", "Les parenth\u00e8ses permettent d'indiquer dans quelle ordre faire les op\u00e9rations dans un calcul:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "3 * (5 + 2) # l'addition est calcul\u00e9e en premier" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "21" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "(3 * 5) + 2 # la multiplication est calcul\u00e9e en premier" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "17" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Sans les parenth\u00e8ses, l'expression est \u00e9valu\u00e9e selon l'ordre de priorit\u00e9 des op\u00e9rations. En particulier, le comportement par d\u00e9faut est que la multiplication est \u00e9valu\u00e9e avant l'addition:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "3 * 5 + 2 # la multiplication est calcul\u00e9e en premier" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "17" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "En g\u00e9n\u00e9ral, les expressions non parenth\u00e9s\u00e9es utilisant les op\u00e9rations de base sont \u00e9valu\u00e9es en tenant compte de l'ordre d\u00e9crit dans la table ci-bas.\n", "\n", "## Variables et affectation\n", "\n", "Supposons que l'on veut \u00e9valuer le polyn\u00f4me $3x^4 + 7x^3 - 3x^2 + x - 5$ lorsque $x=1234567$. On peut proc\u00e9der de la fa\u00e7on suivante:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "3 * 1234567**4 + 7 * 1234567**3 - 3 * 123467**2 + 1234567 - 5" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "6969164759371928046905499" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cela nous oblige \u00e0 \u00e9crire quatre fois le nombre 1234567 et on peut \u00e9viter cela au moyen d'une variable.\n", "\n", "Une variable permet de m\u00e9moriser un nombre pour le r\u00e9utiliser plus tard. Par exemple, on peut m\u00e9moriser le nombre 1234567 dans la variable x :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "x = 1234567" ], "outputs": [], "metadata": {} }, { "source": [ "Le symbole = ne doit pas \u00eatre vu comme une \u00e9quation \u00e0 r\u00e9soudre, mais plut\u00f4t comme une *affectation* de la valeur 1234567 dans la variable x. On peut demander la valeur de x :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "x" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1234567" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Cela nous permet de faire des calculs avec x :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "x + 1" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "1234568" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Finalement, on peut utiliser la variable x pour \u00e9valuer le polyn\u00f4me au point x=1234567 :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "3*x**4 + 7*x**3 - 3*x**2 + x - 5" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "6969164759367401312173299" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "C'est curieux. On remarque que le r\u00e9sultat n'est pas le m\u00eame que celui que l'on avait calcul\u00e9 plus haut. Pourquoi? En effet, on s'\u00e9tait tromp\u00e9 en \u00e9crivant 123467 plut\u00f4t que 1234567. C'est aussi l'autre avantage d'utiliser une variable: \u00e7a permet d'\u00e9viter de se tromper lorsqu'on doit utiliser la m\u00eame valeur plusieurs fois dans un calcul.\n", "\n", "Ensuite, on peut changer la valeur de la variable x et \u00e9valuer le m\u00eame polyn\u00f4me lorsque x prend une autre valeur:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "x = 10\n", "3*x**4 + 7*x**3 - 3*x**2 + x - 5" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "36705" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "## Op\u00e9rateurs de comparaison et d'\u00e9galit\u00e9s\n", "\n", "Comme on l'a vu dans une section pr\u00e9c\u00e9dente, l'op\u00e9rateur = est utilis\u00e9 pour l'affectation de variable. Pour tester l'\u00e9galit\u00e9 de deux expressions, on utilise alors le l'op\u00e9rateur == s'\u00e9crivant avec deux signes d'\u00e9galit\u00e9:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "5 * 9 == 40 + 5" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "La valeur retourn\u00e9e est un bool\u00e9en: True pour vrai et False pour faux. Si l'\u00e9galit\u00e9 n'est pas v\u00e9rifi\u00e9e, alors c'est la valeur False qui est retourn\u00e9e:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "5 * 9 == 40 + 6" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {} } ], "metadata": {} }, { "source": [ "Il existe d'autres op\u00e9rateurs de comparaison dont la description se trouve dans la table ci-bas.\n", "\n", "Par exemple:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "5 * 9 < 1000" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "1 + 2 + 3 + 4 + 5 >= 15" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "2016 != 2016" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {} } ], "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "python2", "name": "python2" } } }