Newer
Older
{
"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,
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"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,
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"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,
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"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",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello world! Hello world! '"
]
},
"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",
"outputs": [],
"source": [
"def hello_world( n: int ) -> str:\n",
" \n",
" return \"Hello world! \" * n"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello world! Hello world! '"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"hello_world(2)"
]
},
{
"cell_type": "code",
"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",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function hello_world in module __main__:\n",
"\n",
"\n"
]
}
],
"source": [
"help(hello_world)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Valeurs par défaut des arguments d'une fonction"
]
},
{
"cell_type": "code",
"outputs": [],
"source": [
"def add(a, b=10):\n",
" return a+b"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add(1, 2)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"N.B. :"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "non-default argument follows default argument (<ipython-input-17-7e1ef5ad4f7a>, line 1)",
"\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",
"outputs": [],
"source": [
"def add(a=1, b=2):\n",
" print(\"a = \", a)\n",
" print(\"b = \", b)\n",
" return a+b"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = 1\n",
"b = 2\n"
]
},
{
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add(1,2)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = 2\n",
"b = 1\n"
]
},
{
"data": {
"text/plain": [
"3"
]
},
"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",
"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",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"arguments:\n",
"(1, 2, 3)\n",
"\n",
"keyword arguments:\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",
"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",
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"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",
"outputs": [],
"source": [
"double = lambda x: x*2"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"double(10)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hello hello '"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"double(\"hello \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exemple d'application"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['apple', 'raspberry', 'orange']"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = ['apple', 'raspberry', 'orange']\n",
"my_list"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"my_list.sort( key = lambda x: len(x) )"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['apple', 'orange', 'raspberry']"
]
},
"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",
}
},
"nbformat": 4,
"nbformat_minor": 2
}