{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "On disait que \n", "* Python est un langage de programmation **interprété, multi-paradigme et multiplateformes**\n", "* Il favorise la **programmation impérative structurée, fonctionnelle et orientée objet**\n", "\n", "Source: https://fr.wikipedia.org/wiki/Python_(langage)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Les fonctions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Déclaration" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def hello_world():\n", " \n", " return \"Hello world!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Appel" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world!'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hello_world()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "a = hello_world()\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Documentation" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function hello_world in module __main__:\n", "\n", "hello_world()\n", "\n" ] } ], "source": [ "help(hello_world)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def hello_world():\n", " \"\"\"\n", " This function sends a welcome message to the user.\n", " \"\"\"\n", " \n", " return \"Hello world!\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function hello_world in module __main__:\n", "\n", "hello_world()\n", " This function sends a welcome message to the user.\n", "\n" ] } ], "source": [ "help(hello_world)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# cf. https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard\n", "\n", "def hello_world( n ):\n", " \"\"\"\n", " This function sends a welcome message to the user.\n", " \n", " Parameters\n", " ----------\n", " n : integer\n", " the number of repetitions\n", "\n", " Returns\n", " -------\n", " string\n", " a value in a string\n", " \"\"\"\n", " \n", " return \"Hello world! \" * n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function hello_world in module __main__:\n", "\n", "hello_world(n)\n", " This function sends a welcome message to the user.\n", " \n", " Parameters\n", " ----------\n", " n : integer\n", " the number of repetitions\n", " \n", " Returns\n", " -------\n", " string\n", " a value in a string\n", "\n" ] } ], "source": [ "help(hello_world)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world! Hello world! '" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hello_world(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### _Type hints_ (Python 3.5+)\n", "cf. https://realpython.com/documenting-python-code/" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def hello_world( n: int ) -> str:\n", " \n", " return \"Hello world! \" * n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello world! Hello world! '" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hello_world(2)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-12-8c7890b5a99f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhello_world\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"2\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m<ipython-input-10-6ece3cdf1a2b>\u001b[0m in \u001b[0;36mhello_world\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mhello_world\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mint\u001b[0m \u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;34m\"Hello world! \"\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'str'" ] } ], "source": [ "hello_world(\"2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "http://mypy-lang.org/ $\\rightarrow$ \"_experimental optional static type checker for Python_\"" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function hello_world in module __main__:\n", "\n", "hello_world(n: int) -> str\n", "\n" ] } ], "source": [ "help(hello_world)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Valeurs par défaut des arguments d'une fonction" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def add(a, b=10):\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(1, 2)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "N.B. :" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "non-default argument follows default argument (<ipython-input-17-7e1ef5ad4f7a>, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"<ipython-input-17-7e1ef5ad4f7a>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def add(a=10, b):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" ] } ], "source": [ "def add(a=10, b):\n", " return a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lors d'un appel, on peut passer les noms des arguments..." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def add(a=1, b=2):\n", " print(\"a = \", a)\n", " print(\"b = \", b)\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = 1\n", "b = 2\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(1,2)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a = 2\n", "b = 1\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(b=1, a=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `*args` et `**kwargs` \n", "N.B. `*toto` et `**tata` marcheraient aussi !" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "def my_function(*args, **kwargs):\n", " print(\"arguments:\")\n", " print(args)\n", " print()\n", " print(\"keyword arguments:\")\n", " print(kwargs)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arguments:\n", "(1, 2, 3)\n", "\n", "keyword arguments:\n", "{'k1': 'v1', 'k2': 'v2'}\n" ] } ], "source": [ "my_function(1, 2, 3, k1=\"v1\", k2=\"v2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oui, on peut faire un mélange :" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "def my_other_function(a, b, *args, **kwargs):\n", " print(\"arguments:\")\n", " print(args)\n", " print()\n", " print(\"keyword arguments:\")\n", " print(kwargs)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arguments:\n", "(3, 4)\n", "\n", "keyword arguments:\n", "{'k1': 'v1'}\n" ] } ], "source": [ "my_other_function(1, 2, 3, 4, k1=\"v1\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Les fonctions `lambda`" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "double = lambda x: x*2" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(10)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello hello '" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(\"hello \")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exemple d'application" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['apple', 'raspberry', 'orange']" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list = ['apple', 'raspberry', 'orange']\n", "my_list" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "my_list.sort( key = lambda x: len(x) )" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['apple', 'orange', 'raspberry']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] } ], "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.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }