Skip to content

Commit c5b8ea1

Browse files
authored
Merge pull request #219 from UCL/remove-python2
Remove mention/use of Python 2 from discussion about types
2 parents a598c48 + b4dcc3f commit c5b8ea1

File tree

1 file changed

+65
-56
lines changed

1 file changed

+65
-56
lines changed

ch00python/023types.ipynb

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"collapsed": true
6767
},
6868
"source": [
69-
"Zero after a point is optional. But the **Dot** makes it a float."
69+
"The zero after a decimal point is optional - it is the **Dot** makes it a float. However, it is better to always include the zero to improve readability."
7070
]
7171
},
7272
{
@@ -142,7 +142,7 @@
142142
"cell_type": "markdown",
143143
"metadata": {},
144144
"source": [
145-
"The meaning of an operator varies depending on the type it is applied to! (And on the python version.)"
145+
"The meaning of an operator varies depending on the type it is applied to!"
146146
]
147147
},
148148
{
@@ -154,12 +154,12 @@
154154
"name": "stdout",
155155
"output_type": "stream",
156156
"text": [
157-
"0\n"
157+
"3\n"
158158
]
159159
}
160160
],
161161
"source": [
162-
"print(one // ten)"
162+
"print(1 + 2) # returns an integer"
163163
]
164164
},
165165
{
@@ -168,18 +168,22 @@
168168
"metadata": {},
169169
"outputs": [
170170
{
171-
"data": {
172-
"text/plain": [
173-
"0.1"
174-
]
175-
},
176-
"execution_count": 8,
177-
"metadata": {},
178-
"output_type": "execute_result"
171+
"name": "stdout",
172+
"output_type": "stream",
173+
"text": [
174+
"3.0\n"
175+
]
179176
}
180177
],
181178
"source": [
182-
"one_float / ten_float"
179+
"print(1.0 + 2.0) # returns a float"
180+
]
181+
},
182+
{
183+
"cell_type": "markdown",
184+
"metadata": {},
185+
"source": [
186+
"The division by operator always returns a `float`, whether it's applied to `float`s or `int`s."
183187
]
184188
},
185189
{
@@ -188,15 +192,18 @@
188192
"metadata": {},
189193
"outputs": [
190194
{
191-
"name": "stdout",
192-
"output_type": "stream",
193-
"text": [
194-
"<class 'float'>\n"
195-
]
195+
"data": {
196+
"text/plain": [
197+
"3.3333333333333335"
198+
]
199+
},
200+
"execution_count": 9,
201+
"metadata": {},
202+
"output_type": "execute_result"
196203
}
197204
],
198205
"source": [
199-
"print(type(one / ten))"
206+
"10 / 3"
200207
]
201208
},
202209
{
@@ -207,7 +214,7 @@
207214
{
208215
"data": {
209216
"text/plain": [
210-
"float"
217+
"3.3333333333333335"
211218
]
212219
},
213220
"execution_count": 10,
@@ -216,15 +223,7 @@
216223
}
217224
],
218225
"source": [
219-
"type(tenth)"
220-
]
221-
},
222-
{
223-
"cell_type": "markdown",
224-
"metadata": {},
225-
"source": [
226-
"The divided by operator when applied to floats, means divide by for real numbers. But when applied to integers, it means\n",
227-
"divide then round down:"
226+
"10.0 / 3"
228227
]
229228
},
230229
{
@@ -235,7 +234,7 @@
235234
{
236235
"data": {
237236
"text/plain": [
238-
"3"
237+
"3.3333333333333335"
239238
]
240239
},
241240
"execution_count": 11,
@@ -244,7 +243,14 @@
244243
}
245244
],
246245
"source": [
247-
"10 // 3"
246+
"10 / 3.0"
247+
]
248+
},
249+
{
250+
"cell_type": "markdown",
251+
"metadata": {},
252+
"source": [
253+
"To perform integer division we need to use the `divmod` function, which returns the quotiant and remainder of the division."
248254
]
249255
},
250256
{
@@ -253,18 +259,23 @@
253259
"metadata": {},
254260
"outputs": [
255261
{
256-
"data": {
257-
"text/plain": [
258-
"3.3333333333333335"
259-
]
260-
},
261-
"execution_count": 12,
262-
"metadata": {},
263-
"output_type": "execute_result"
262+
"name": "stdout",
263+
"output_type": "stream",
264+
"text": [
265+
"quotiant=3, remainder=1\n"
266+
]
264267
}
265268
],
266269
"source": [
267-
"10.0 / 3"
270+
"quotiant, remainder = divmod(10, 3)\n",
271+
"print(f\"{quotiant=}, {remainder=}\")"
272+
]
273+
},
274+
{
275+
"cell_type": "markdown",
276+
"metadata": {},
277+
"source": [
278+
"Note that if either of the input type are `float`, the returned values will also be `float`s."
268279
]
269280
},
270281
{
@@ -275,7 +286,7 @@
275286
{
276287
"data": {
277288
"text/plain": [
278-
"3.3333333333333335"
289+
"(3.0, 1.0)"
279290
]
280291
},
281292
"execution_count": 13,
@@ -284,21 +295,14 @@
284295
}
285296
],
286297
"source": [
287-
"10 / 3.0"
298+
"divmod(10, 3.0)"
288299
]
289300
},
290301
{
291302
"cell_type": "markdown",
292303
"metadata": {},
293304
"source": [
294-
"So if I have two integer variables, and I want the `float` division, I need to change the type first."
295-
]
296-
},
297-
{
298-
"cell_type": "markdown",
299-
"metadata": {},
300-
"source": [
301-
"There is a function for every type name, which is used to convert the input to an output of the desired type."
305+
"There is a function for every built-in type, which is used to convert the input to an output of the desired type."
302306
]
303307
},
304308
{
@@ -330,7 +334,7 @@
330334
{
331335
"data": {
332336
"text/plain": [
333-
"3.3333333333333335"
337+
"(3.0, 1.0)"
334338
]
335339
},
336340
"execution_count": 15,
@@ -339,7 +343,7 @@
339343
}
340344
],
341345
"source": [
342-
"10 / float(3)"
346+
"divmod(10, float(3))"
343347
]
344348
},
345349
{
@@ -1241,7 +1245,7 @@
12411245
"traceback": [
12421246
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
12431247
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
1244-
"\u001b[0;32m<ipython-input-52-3331a3ab5222>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mzero\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtwo\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mthree\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1248+
"Cell \u001b[0;32mIn [52], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m zero, one, two, three \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m7\u001b[39m)\n",
12451249
"\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 4)"
12461250
]
12471251
}
@@ -1262,7 +1266,7 @@
12621266
"traceback": [
12631267
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
12641268
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
1265-
"\u001b[0;32m<ipython-input-53-8575e9410b1d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mzero\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtwo\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mthree\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1269+
"Cell \u001b[0;32mIn [53], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m zero, one, two, three \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m2\u001b[39m)\n",
12661270
"\u001b[0;31mValueError\u001b[0m: not enough values to unpack (expected 4, got 2)"
12671271
]
12681272
}
@@ -1341,7 +1345,7 @@
13411345
"display_name": "Types"
13421346
},
13431347
"kernelspec": {
1344-
"display_name": "Python 3",
1348+
"display_name": "Python 3 (ipykernel)",
13451349
"language": "python",
13461350
"name": "python3"
13471351
},
@@ -1355,7 +1359,12 @@
13551359
"name": "python",
13561360
"nbconvert_exporter": "python",
13571361
"pygments_lexer": "ipython3",
1358-
"version": "3.7.3"
1362+
"version": "3.9.13"
1363+
},
1364+
"vscode": {
1365+
"interpreter": {
1366+
"hash": "56f3b33ce8ef81dba99c545090023eafaf7aedb33c344d352a9ab6fb4e2c3676"
1367+
}
13591368
}
13601369
},
13611370
"nbformat": 4,

0 commit comments

Comments
 (0)