{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Le module math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pour importer le module math" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les multiplications\n", "Ne pas oublier les multiplications entre les constantes et les variables." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 2x+3 n'est pas accepté\n", "def f(x):\n", " return 2*x+3\n", "f(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les puissances" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "150" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x):\n", " return x**3+x**2\n", "f(5)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "150.0" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x):\n", " return math.pow(x,3)+math.pow(x,2)\n", "f(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## La racine carrée" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.0\n" ] } ], "source": [ "# en utilisant le module math\n", "a=math.sqrt(9)\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.0\n" ] } ], "source": [ "# avec la fonction puissance\n", "a=9**0.5\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les divisions" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "# la division euclidienne\n", "b=13//5\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.6\n" ] } ], "source": [ "# la division décimale\n", "b=13/5\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les fonctions exponentielle et logarithme népérien" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.718281828459045\n" ] } ], "source": [ "# fonction exp\n", "c=math.exp(1)\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.6931471805599453\n" ] } ], "source": [ "#fonction ln \n", "d=math.log(2)\n", "print(d)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.718281828459045\n" ] } ], "source": [ "# constante e\n", "e=math.e\n", "print(e)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0\n", "6.0\n" ] } ], "source": [ "# fonction log de base a\n", "a=math.log(100,10)\n", "print(a)\n", "b=math.log(64,2)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les fonctions trigonométriques\n", "Ces fonctions travaillent en radians." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793\n" ] } ], "source": [ "# constante PI\n", "p=math.pi\n", "print(p)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0\n", "1.2246467991473532e-16\n", "0.9999999999999999\n" ] } ], "source": [ "# fonctions cos, sin, tan\n", "a=math.cos(0)\n", "print(a)\n", "b=math.sin(math.pi) # presque zéro ...\n", "print(b)\n", "c=math.tan(math.pi/4) # presque 1 ...\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "180.0\n", "1.5707963267948966\n" ] } ], "source": [ "# convertir en degrés\n", "a=math.degrees(math.pi)\n", "print(a)\n", "# convertir en radians\n", "b=math.radians(90)\n", "print(b)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quelques fonctions arithmétiques" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "5\n" ] } ], "source": [ "# valeur entière par excès\n", "a=math.ceil(5.8)\n", "print(a)\n", "# valeur entière par défaut\n", "b=math.floor(5.8)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n" ] } ], "source": [ "# factoriel\n", "a=math.factorial(5)\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "# pgcd\n", "a=math.gcd(18,45)\n", "print(a)" ] }, { "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": [] } ], "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", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }