{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "[prev: Noms et variables](noms-variables.ipynb) | [home](../index.ipynb) | [next: *type()* et *help()* : introspection](introspection.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction aux fonctions\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def add(x, y):\n", " \"additionne x et y\"\n", " result = x + y\n", " return result" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(1, 2)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bonjour'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add('bon', 'jour')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function add in module __main__:\n", "\n", "add(x, y)\n", " additionne x et y\n", "\n" ] } ], "source": [ "help(add)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 7 10\n" ] } ], "source": [ "a = add(1, 2)\n", "b = add(3, 4)\n", "c = add(a, b)\n", "print(a, b, c)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(1, add(1, add(1, add(1, 1))))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import IFrame\n", "IFrame(\"https://pythontutor.com/iframe-embed.html#code=def%20add%28x,%20y%29%3A%0A%20%20%20%20%22additionne%20x%20et%20y%22%0A%20%20%20%20result%20%3D%20x%20%2B%20y%0A%20%20%20%20return%20result%0A%0Aa%20%3D%20add%281,%202%29%0Ab%20%3D%20add%28a,%2010%29%0A&codeDivHeight=400&codeDivWidth=350&cumulative=true&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false\",\n", " width='100%', height=400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[lien](http://pythontutor.com/visualize.html#code=def%20add%28x,%20y%29%3A%0A%20%20%20%20%22additionne%20x%20et%20y%22%0A%20%20%20%20result%20%3D%20x%20%2B%20y%0A%20%20%20%20return%20result%0A%0Aa%20%3D%20add%281,%202%29%0Ab%20%3D%20add%28a,%2010%29%0A&cumulative=true&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fonctions sans retour" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bonjour\n", "42\n", "None None\n" ] } ], "source": [ "def ne_renvoie_rien(arg):\n", " \"affiche arg\"\n", " print(arg)\n", "\n", "a = ne_renvoie_rien('bonjour')\n", "b = ne_renvoie_rien(42)\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bonjour\n", "42\n", "None None\n" ] } ], "source": [ "def ne_renvoie_rien_bis(arg):\n", " \"affiche arg\"\n", " print(arg)\n", " return\n", "\n", "a = ne_renvoie_rien_bis('bonjour')\n", "b = ne_renvoie_rien_bis(42)\n", "print(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## None ?\n", "- `None` sert à représenter le rien (ce qui ne sert pas à rien)\n", "- Il est unique\n", "- A part sa conversion en booléen (`False`), il ne fonctionne avec aucun opérateur\n", "- On teste si une variable est None avec `x is None`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(None)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "int() argument must be a string, a bytes-like object or a real number, not 'NoneType'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[11], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\n", "\u001b[1;31mTypeError\u001b[0m: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'" ] } ], "source": [ "int(None)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'NoneType' and 'int'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[12], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'NoneType' and 'int'" ] } ], "source": [ "None + 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Fonction double](../exercises/exercises.ipynb#Exercice-1-:-double)\n", "- [Conversion de température](../exercises/exercises.ipynb#Exercice-1-:-double)\n", "- [Conversion de secondes (étape 1)](../exercises/exercises.ipynb#Exercice-3-:-conversion-de-secondes)" ] } ], "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.12" } }, "nbformat": 4, "nbformat_minor": 4 }