Skip to content

Commit 52ca70b

Browse files
committed
Pyupgrade 3.10+
1 parent 5a0793d commit 52ca70b

24 files changed

+167
-199
lines changed

notebooks/beginner/exercises/01_strings_exercise.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
},
8585
"outputs": [],
8686
"source": [
87-
"print(\"pretty: {}\".format(pretty))\n",
87+
"print(f\"pretty: {pretty}\")\n",
8888
"assert pretty == \"Title Of My New Book\""
8989
]
9090
},
@@ -127,7 +127,7 @@
127127
},
128128
"outputs": [],
129129
"source": [
130-
"print(\"sentence: {}\".format(sentence))\n",
130+
"print(f\"sentence: {sentence}\")\n",
131131
"assert sentence == \"Learning Python is fun!\""
132132
]
133133
}

notebooks/beginner/exercises/03_conditionals_exercise.ipynb

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@
2626
"outputs": [],
2727
"source": [
2828
"if ____:\n",
29-
" print('Name \"{}\" is more than 20 chars long'.format(name))\n",
29+
" print(f'Name \"{name}\" is more than 20 chars long')\n",
3030
" length_description = \"long\"\n",
3131
"elif ____:\n",
32-
" print('Name \"{}\" is more than 15 chars long'.format(name))\n",
32+
" print(f'Name \"{name}\" is more than 15 chars long')\n",
3333
" length_description = \"semi long\"\n",
3434
"elif ____:\n",
35-
" print('Name \"{}\" is more than 10 chars long'.format(name))\n",
35+
" print(f'Name \"{name}\" is more than 10 chars long')\n",
3636
" length_description = \"semi long\"\n",
3737
"elif ____:\n",
38-
" print('Name \"{}\" is 8, 9 or 10 chars long'.format(name))\n",
38+
" print(f'Name \"{name}\" is 8, 9 or 10 chars long')\n",
3939
" length_description = \"semi short\"\n",
4040
"else:\n",
41-
" print('Name \"{}\" is a short name'.format(name))\n",
41+
" print(f'Name \"{name}\" is a short name')\n",
4242
" length_description = \"short\""
4343
]
4444
},

notebooks/beginner/exercises/16_testing2_exercise.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@
6060
"\n",
6161
" def complete(self, number):\n",
6262
" if number not in self._todo:\n",
63-
" raise TodoNotFound(\"{} not in todos\".format(number))\n",
63+
" raise TodoNotFound(f\"{number} not in todos\")\n",
6464
"\n",
6565
" task = self._todo.pop(number)\n",
6666
" self._done[number] = task\n",
6767
"\n",
6868
" def remove(self, number):\n",
6969
" if number not in self._todo:\n",
70-
" raise TodoNotFound(\"{} not in todos\".format(number))\n",
70+
" raise TodoNotFound(f\"{number} not in todos\")\n",
7171
"\n",
7272
" del self._todo[number]"
7373
]

notebooks/beginner/notebooks/01_strings.ipynb

+15-18
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"cell_type": "markdown",
127127
"metadata": {},
128128
"source": [
129-
"## `str.format()`"
129+
"## f-strings"
130130
]
131131
},
132132
{
@@ -135,8 +135,11 @@
135135
"metadata": {},
136136
"outputs": [],
137137
"source": [
138-
"secret = \"{} is cool\".format(\"Python\")\n",
139-
"print(secret)"
138+
"first_name = \"John\"\n",
139+
"last_name = \"Doe\"\n",
140+
"age = 88\n",
141+
"print(f\"My name is {first_name} {last_name}, you can call me {first_name}.\")\n",
142+
"print(f\"I'm {age} years old.\")"
140143
]
141144
},
142145
{
@@ -145,13 +148,7 @@
145148
"metadata": {},
146149
"outputs": [],
147150
"source": [
148-
"print(\"My name is {} {}, you can call me {}.\".format(\"John\", \"Doe\", \"John\"))\n",
149-
"# is the same as:\n",
150-
"print(\n",
151-
" \"My name is {first} {family}, you can call me {first}.\".format(\n",
152-
" first=\"John\", family=\"Doe\"\n",
153-
" )\n",
154-
")"
151+
"print(f\"Use '=' to also print the variable name like this: {age=}\")"
155152
]
156153
},
157154
{
@@ -179,14 +176,14 @@
179176
"metadata": {},
180177
"outputs": [],
181178
"source": [
182-
"print(\"Some cool python libraries: {}\".format(cool_python_libs))"
179+
"print(f\"Some cool python libraries: {cool_python_libs}\")"
183180
]
184181
},
185182
{
186183
"cell_type": "markdown",
187184
"metadata": {},
188185
"source": [
189-
"Alternatives (not as [Pythonic](http://docs.python-guide.org/en/latest/writing/style/#idioms) and [slower](https://waymoot.org/home/python_string/)):"
186+
"Alternative (not as [Pythonic](http://docs.python-guide.org/en/latest/writing/style/#idioms) and [slower](https://waymoot.org/home/python_string/)):"
190187
]
191188
},
192189
{
@@ -196,12 +193,12 @@
196193
"outputs": [],
197194
"source": [
198195
"cool_python_libs = pandas + \", \" + numpy + \", \" + requests\n",
199-
"print(\"Some cool python libraries: {}\".format(cool_python_libs))\n",
196+
"print(f\"Some cool python libraries: {cool_python_libs}\")\n",
200197
"\n",
201198
"cool_python_libs = pandas\n",
202199
"cool_python_libs += \", \" + numpy\n",
203200
"cool_python_libs += \", \" + requests\n",
204-
"print(\"Some cool python libraries: {}\".format(cool_python_libs))"
201+
"print(f\"Some cool python libraries: {cool_python_libs}\")"
205202
]
206203
},
207204
{
@@ -263,8 +260,8 @@
263260
"ugly_formatted = \" \\n \\t Some story to tell \"\n",
264261
"stripped = ugly_formatted.strip()\n",
265262
"\n",
266-
"print(\"ugly: {}\".format(ugly_formatted))\n",
267-
"print(\"stripped: {}\".format(ugly_formatted.strip()))"
263+
"print(f\"ugly: {ugly_formatted}\")\n",
264+
"print(f\"stripped: {stripped}\")"
268265
]
269266
},
270267
{
@@ -370,7 +367,7 @@
370367
],
371368
"metadata": {
372369
"kernelspec": {
373-
"display_name": "Python 3",
370+
"display_name": "Python 3 (ipykernel)",
374371
"language": "python",
375372
"name": "python3"
376373
},
@@ -384,7 +381,7 @@
384381
"name": "python",
385382
"nbconvert_exporter": "python",
386383
"pygments_lexer": "ipython3",
387-
"version": "3.5.4"
384+
"version": "3.11.0"
388385
}
389386
},
390387
"nbformat": 4,

notebooks/beginner/notebooks/02_numbers.ipynb

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"outputs": [],
2222
"source": [
2323
"my_int = 6\n",
24-
"print(\"value: {}, type: {}\".format(my_int, type(my_int)))"
24+
"print(f\"value: {my_int}, type: {type(my_int)}\")"
2525
]
2626
},
2727
{
@@ -38,7 +38,7 @@
3838
"outputs": [],
3939
"source": [
4040
"my_float = float(my_int)\n",
41-
"print(\"value: {}, type: {}\".format(my_float, type(my_float)))"
41+
"print(f\"value: {my_float}, type: {type(my_float)}\")"
4242
]
4343
},
4444
{
@@ -135,7 +135,7 @@
135135
"source": [
136136
"from_float = Decimal(0.1)\n",
137137
"from_str = Decimal(\"0.1\")\n",
138-
"print(\"from float: {}\\nfrom string: {}\".format(from_float, from_str))"
138+
"print(f\"from float: {from_float}\\nfrom string: {from_str}\")"
139139
]
140140
},
141141
{
@@ -170,7 +170,7 @@
170170
],
171171
"metadata": {
172172
"kernelspec": {
173-
"display_name": "Python 3",
173+
"display_name": "Python 3 (ipykernel)",
174174
"language": "python",
175175
"name": "python3"
176176
},
@@ -184,7 +184,7 @@
184184
"name": "python",
185185
"nbconvert_exporter": "python",
186186
"pygments_lexer": "ipython3",
187-
"version": "3.5.4"
187+
"version": "3.11.0"
188188
}
189189
},
190190
"nbformat": 4,

notebooks/beginner/notebooks/03_conditionals.ipynb

+21-46
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
{
22
"cells": [
3-
{
4-
"cell_type": "code",
5-
"execution_count": null,
6-
"metadata": {},
7-
"outputs": [],
8-
"source": [
9-
"from IPython.display import HTML\n",
10-
"\n",
11-
"HTML(\n",
12-
" \"\"\"<script>\n",
13-
"code_show=true; \n",
14-
"function code_toggle() {\n",
15-
" if (code_show){\n",
16-
" $('div.jp-OutputArea').hide();\n",
17-
" } else {\n",
18-
" $('div.jp-OutputArea').show();\n",
19-
" }\n",
20-
" code_show = !code_show\n",
21-
"} \n",
22-
"$( document ).ready(code_toggle);\n",
23-
"</script>\n",
24-
"<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code.\"></form>\"\"\"\n",
25-
")"
26-
]
27-
},
283
{
294
"cell_type": "markdown",
305
"metadata": {},
@@ -52,7 +27,7 @@
5227
"metadata": {},
5328
"outputs": [],
5429
"source": [
55-
"print(\"type of True and False: {}\".format(type(True)))"
30+
"print(f\"type of True and False: {type(True)}\")"
5631
]
5732
},
5833
{
@@ -61,9 +36,9 @@
6136
"metadata": {},
6237
"outputs": [],
6338
"source": [
64-
"print(\"0: {}, 1: {}\".format(bool(0), bool(1)))\n",
65-
"print(\"empty list: {}, list with values: {}\".format(bool([]), bool([\"woop\"])))\n",
66-
"print(\"empty dict: {}, dict with values: {}\".format(bool({}), bool({\"Python\": \"cool\"})))"
39+
"print(f\"0: {bool(0)}, 1: {bool(1)}\")\n",
40+
"print(f\"empty list: {bool([])}, list with values: {bool(['woop'])}\")\n",
41+
"print(f\"empty dict: {bool({})}, dict with values: {bool({'Python': 'cool'})}\")"
6742
]
6843
},
6944
{
@@ -79,16 +54,16 @@
7954
"metadata": {},
8055
"outputs": [],
8156
"source": [
82-
"print(\"1 == 0: {}\".format(1 == 0))\n",
83-
"print(\"1 != 0: {}\".format(1 != 0))\n",
84-
"print(\"1 > 0: {}\".format(1 > 0))\n",
85-
"print(\"1 > 1: {}\".format(1 > 1))\n",
86-
"print(\"1 < 0: {}\".format(1 < 0))\n",
87-
"print(\"1 < 1: {}\".format(1 < 1))\n",
88-
"print(\"1 >= 0: {}\".format(1 >= 0))\n",
89-
"print(\"1 >= 1: {}\".format(1 >= 1))\n",
90-
"print(\"1 <= 0: {}\".format(1 <= 0))\n",
91-
"print(\"1 <= 1: {}\".format(1 <= 1))"
57+
"print(f\"{1 == 0}\")\n",
58+
"print(f\"{1 != 0}\")\n",
59+
"print(f\"{1 > 0}\")\n",
60+
"print(f\"{1 > 1}\")\n",
61+
"print(f\"{1 < 0}\")\n",
62+
"print(f\"{1 < 1}\")\n",
63+
"print(f\"{1 >= 0}\")\n",
64+
"print(f\"{1 >= 1}\")\n",
65+
"print(f\"{1 <= 0}\")\n",
66+
"print(f\"{1 <= 1}\")"
9267
]
9368
},
9469
{
@@ -104,7 +79,7 @@
10479
"metadata": {},
10580
"outputs": [],
10681
"source": [
107-
"print(\"1 <= 2 <= 3: {}\".format(1 <= 2 <= 3))"
82+
"print(f\"{1 <= 2 <= 3}\")"
10883
]
10984
},
11085
{
@@ -132,8 +107,8 @@
132107
"metadata": {},
133108
"outputs": [],
134109
"source": [
135-
"print(\"Python and java are both cool: {}\".format(python_is_cool and java_is_cool))\n",
136-
"print(\"secret_value and python_is_cool: {}\".format(secret_value and python_is_cool))"
110+
"print(f\"Python and java are both cool: {python_is_cool and java_is_cool}\")\n",
111+
"print(f\"secret_value and python_is_cool: {secret_value and python_is_cool}\")"
137112
]
138113
},
139114
{
@@ -142,8 +117,8 @@
142117
"metadata": {},
143118
"outputs": [],
144119
"source": [
145-
"print(\"Python or java is cool: {}\".format(python_is_cool or java_is_cool))\n",
146-
"print('1 >= 1.1 or 2 < float(\"1.4\"): {}'.format(1 >= 1.1 or 2 < float(\"1.4\")))"
120+
"print(f\"Python or java is cool: {python_is_cool or java_is_cool}\")\n",
121+
"print(f\"{1 >= 1.1 or 2 < 1.4}\")"
147122
]
148123
},
149124
{
@@ -152,7 +127,7 @@
152127
"metadata": {},
153128
"outputs": [],
154129
"source": [
155-
"print(\"Java is not cool: {}\".format(not java_is_cool))"
130+
"print(f\"Java is not cool: {not java_is_cool}\")"
156131
]
157132
},
158133
{
@@ -310,7 +285,7 @@
310285
"name": "python",
311286
"nbconvert_exporter": "python",
312287
"pygments_lexer": "ipython3",
313-
"version": "3.10.3"
288+
"version": "3.11.0"
314289
}
315290
},
316291
"nbformat": 4,

0 commit comments

Comments
 (0)