Skip to content

Commit

Permalink
Refactor before sending to students.
Browse files Browse the repository at this point in the history
  • Loading branch information
phe-sto committed Oct 29, 2024
1 parent e85c486 commit cacbab5
Show file tree
Hide file tree
Showing 4 changed files with 702 additions and 195 deletions.
15 changes: 8 additions & 7 deletions 2_base_programmation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@
"metadata": {
"trusted": false,
"ExecuteTime": {
"end_time": "2024-10-15T13:12:05.927679Z",
"start_time": "2024-10-15T13:12:05.885039Z"
"end_time": "2024-10-25T15:36:39.333391Z",
"start_time": "2024-10-25T15:36:39.290265Z"
}
},
"cell_type": "code",
Expand Down Expand Up @@ -246,8 +246,8 @@
"metadata": {
"trusted": false,
"ExecuteTime": {
"end_time": "2024-10-15T13:12:05.940537Z",
"start_time": "2024-10-15T13:12:05.934294Z"
"end_time": "2024-10-25T15:36:39.345249Z",
"start_time": "2024-10-25T15:36:39.339260Z"
}
},
"cell_type": "code",
Expand Down Expand Up @@ -282,8 +282,8 @@
"metadata": {
"trusted": false,
"ExecuteTime": {
"end_time": "2024-10-15T13:12:05.962319Z",
"start_time": "2024-10-15T13:12:05.956248Z"
"end_time": "2024-10-25T15:36:39.363125Z",
"start_time": "2024-10-25T15:36:39.356733Z"
}
},
"cell_type": "code",
Expand Down Expand Up @@ -329,7 +329,8 @@
"cell_type": "markdown",
"source": [
"Écrire un script Python appelé `leap_year.py` qui vérifie si une année saisie par l'utilisateur est une année bissextile ou non.\n",
"Sachant qu'une année est bissextile si elle est divisible par 4 ou 100 ou 400.\n",
"Sachant qu'une année est bissextile si elle est divisible par 4 mais pas par 100 à l'exception\n",
" de celles divisible par 400 (voir https://fr.wikipedia.org/wiki/Ann%C3%A9e_bissextile).\n",
"- Attention aux commentaires\n",
"- Attention aux noms de variables. \n",
"*Faites valider votre script ainsi que son exécution*. "
Expand Down
91 changes: 74 additions & 17 deletions 3_boucles_iteratives.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"metadata": {
"trusted": false,
"ExecuteTime": {
"end_time": "2024-10-15T13:25:18.677732Z",
"start_time": "2024-10-15T13:25:18.325994Z"
"end_time": "2024-10-25T15:36:33.991978Z",
"start_time": "2024-10-25T15:36:33.698158Z"
}
},
"cell_type": "code",
Expand Down Expand Up @@ -76,11 +76,7 @@
},
{
"metadata": {
"trusted": false,
"ExecuteTime": {
"end_time": "2024-10-15T13:25:18.682650Z",
"start_time": "2024-10-15T13:25:18.682322Z"
}
"trusted": false
},
"cell_type": "code",
"source": [
Expand Down Expand Up @@ -202,11 +198,7 @@
},
{
"metadata": {
"trusted": false,
"ExecuteTime": {
"end_time": "2024-10-15T13:25:18.687959Z",
"start_time": "2024-10-15T13:25:18.687654Z"
}
"trusted": false
},
"cell_type": "code",
"source": "#Syntaxe d'écriture\nfor variable in collection:\n instruction1",
Expand Down Expand Up @@ -356,6 +348,10 @@
"\n",
"current_year = datetime.datetime.now().year\n",
"for year in range(current_year, current_year + 100):\n",
" \"\"\"\n",
" According to https://fr.wikipedia.org/wiki/Ann%C3%A9e_bissextile\n",
" Les années sont bissextiles si elles sont multiples de quatre, mais pas si elles sont multiples de cent, à l'exception des années multiples de quatre cents qui, elles, sont également bissextiles. Ainsi, les années 2020, 2024 et 2028 sont bissextiles, de même que 2000 ou 2400, mais pas 1900, 2100, 2200 et 2300. \n",
" \"\"\"\n",
" if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:\n",
" break\n",
"print(\"La dernière année bissextile est: \", year)"
Expand Down Expand Up @@ -521,10 +517,10 @@
"# Exemple de l'utilisation de map\n",
"# On met au carré chaque élément de la liste\n",
"numbers = [1, 2, 3, 4]\n",
"squared_numbers = list(map(lambda x: x**2, numbers))\n",
"squared_numbers = list(map(lambda x: x ** 2, numbers))\n",
"print(squared_numbers)\n",
"# On a besoin de list() map est un objet qui n'affiche pas les valeurs\n",
"map_squared_numbers = map(lambda x: x**2, numbers)\n",
"map_squared_numbers = map(lambda x: x ** 2, numbers)\n",
"print(\"Object \", map_squared_numbers, \"is of type: \", type(map_squared_numbers))\n",
"# Mais il est itérable\n",
"for number in map_squared_numbers:\n",
Expand All @@ -548,14 +544,23 @@
"# Exemple de l'utilisation de map\n",
"# On met au carré chaque élément de la liste\n",
"numbers = [1, 2, 3, 4]\n",
"squared_numbers = [x**2 for x in numbers]\n",
"squared_numbers = [x ** 2 for x in numbers]\n",
"print(squared_numbers)\n",
"# On peut filtrer les éléments\n",
"even_numbers = [x for x in numbers if x % 2 == 0]\n",
"print(even_numbers)"
],
"outputs": [],
"execution_count": null
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16]\n",
"[2, 4]\n"
]
}
],
"execution_count": 2
},
{
"metadata": {},
Expand All @@ -564,6 +569,58 @@
"Les performances peuvent être meilleures avec `map` mais dans certains cas seulement, il faut voir au cas par cas.\n",
"Voir le ticket StackOverflow https://stackoverflow.com/questions/1247486/list-comprehension-vs-map."
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"***\n",
"## Unpack avec l'operateur splat `*`\n",
"\n",
"### Unpack d'iterables\n",
"\n",
"On peut créer un nouveau itérable avec `*` et une autre valeur :"
]
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-29T22:26:57.520669Z",
"start_time": "2024-10-29T22:26:57.518362Z"
}
},
"cell_type": "code",
"source": [
"assert [1, *[2, 3, 4]] == [1, 2, 3, 4] # Un itérable et une valeur\n",
"assert [*[1, 2], *[3, 4]] == [1, 2, 3, 4] # 2 Itérables\n",
"assert [*[1, 2], *(3, 4)] == [1, 2, 3, 4] # 2 Itérables, tuple ou liste"
],
"outputs": [],
"execution_count": 3
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### Unpack de dictionnaires avec `**`\n",
"\n",
"Il est également possible de manipuler les dictionnaires pour en créer de nouveau avec `*` :"
]
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-29T22:29:57.962047Z",
"start_time": "2024-10-29T22:29:57.959603Z"
}
},
"cell_type": "code",
"source": [
"assert {1: 1, **{2: 2, 3: 3}} == {1: 1, 2: 2, 3: 3}\n",
"assert {**{1: 1, 2: 2}, **{3: 3}} == {1: 1, 2: 2, 3: 3}"
],
"outputs": [],
"execution_count": 5
}
],
"metadata": {
Expand Down
Loading

0 comments on commit cacbab5

Please sign in to comment.