Skip to content

Commit 295db2c

Browse files
Paul Smithp-j-smith
authored andcommitted
Remove mention/use of Python 2 from discussion about types.
1 parent f16e618 commit 295db2c

File tree

1 file changed

+72
-63
lines changed

1 file changed

+72
-63
lines changed

ch00python/023types.ipynb

Lines changed: 72 additions & 63 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,109 +142,68 @@
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
{
149149
"cell_type": "code",
150-
"execution_count": 7,
150+
"execution_count": 1,
151151
"metadata": {},
152152
"outputs": [
153153
{
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
{
166166
"cell_type": "code",
167-
"execution_count": 8,
168-
"metadata": {},
169-
"outputs": [
170-
{
171-
"data": {
172-
"text/plain": [
173-
"0.1"
174-
]
175-
},
176-
"execution_count": 8,
177-
"metadata": {},
178-
"output_type": "execute_result"
179-
}
180-
],
181-
"source": [
182-
"one_float / ten_float"
183-
]
184-
},
185-
{
186-
"cell_type": "code",
187-
"execution_count": 9,
167+
"execution_count": 2,
188168
"metadata": {},
189169
"outputs": [
190170
{
191171
"name": "stdout",
192172
"output_type": "stream",
193173
"text": [
194-
"<class 'float'>\n"
174+
"3.0\n"
195175
]
196176
}
197177
],
198178
"source": [
199-
"print(type(one / ten))"
200-
]
201-
},
202-
{
203-
"cell_type": "code",
204-
"execution_count": 10,
205-
"metadata": {},
206-
"outputs": [
207-
{
208-
"data": {
209-
"text/plain": [
210-
"float"
211-
]
212-
},
213-
"execution_count": 10,
214-
"metadata": {},
215-
"output_type": "execute_result"
216-
}
217-
],
218-
"source": [
219-
"type(tenth)"
179+
"print(1.0 + 2.0) # returns a float"
220180
]
221181
},
222182
{
223183
"cell_type": "markdown",
224184
"metadata": {},
225185
"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:"
186+
"The division by operator always returns a `float`, whether it's applied to `float`s or `int`s."
228187
]
229188
},
230189
{
231190
"cell_type": "code",
232-
"execution_count": 11,
191+
"execution_count": 4,
233192
"metadata": {},
234193
"outputs": [
235194
{
236195
"data": {
237196
"text/plain": [
238-
"3"
197+
"3.3333333333333335"
239198
]
240199
},
241-
"execution_count": 11,
200+
"execution_count": 4,
242201
"metadata": {},
243202
"output_type": "execute_result"
244203
}
245204
],
246205
"source": [
247-
"10 // 3"
206+
"10 / 3"
248207
]
249208
},
250209
{
@@ -291,14 +250,32 @@
291250
"cell_type": "markdown",
292251
"metadata": {},
293252
"source": [
294-
"So if I have two integer variables, and I want the `float` division, I need to change the type first."
253+
"To perform integer division we need to use the `divmod` function, which returns the quotiant and remainder of the division."
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": 12,
259+
"metadata": {},
260+
"outputs": [
261+
{
262+
"name": "stdout",
263+
"output_type": "stream",
264+
"text": [
265+
"quotiant=3, remainder=1\n"
266+
]
267+
}
268+
],
269+
"source": [
270+
"quotiant, remainder = divmod(10, 3)\n",
271+
"print(f\"{quotiant=}, {remainder=}\")"
295272
]
296273
},
297274
{
298275
"cell_type": "markdown",
299276
"metadata": {},
300277
"source": [
301-
"There is a function for every type name, which is used to convert the input to an output of the desired type."
278+
"Note that if either of the input type are `float`, the returned values will also be `float`s."
302279
]
303280
},
304281
{
@@ -309,37 +286,64 @@
309286
{
310287
"data": {
311288
"text/plain": [
312-
"float"
289+
"(3.0, 1.0)"
313290
]
314291
},
315292
"execution_count": 14,
316293
"metadata": {},
317294
"output_type": "execute_result"
318295
}
319296
],
297+
"source": [
298+
"divmod(10, 3.0)"
299+
]
300+
},
301+
{
302+
"cell_type": "markdown",
303+
"metadata": {},
304+
"source": [
305+
"There is a function for every built-in type, which is used to convert the input to an output of the desired type."
306+
]
307+
},
308+
{
309+
"cell_type": "code",
310+
"execution_count": 16,
311+
"metadata": {},
312+
"outputs": [
313+
{
314+
"data": {
315+
"text/plain": [
316+
"float"
317+
]
318+
},
319+
"execution_count": 16,
320+
"metadata": {},
321+
"output_type": "execute_result"
322+
}
323+
],
320324
"source": [
321325
"x = float(5)\n",
322326
"type(x)"
323327
]
324328
},
325329
{
326330
"cell_type": "code",
327-
"execution_count": 15,
331+
"execution_count": 18,
328332
"metadata": {},
329333
"outputs": [
330334
{
331335
"data": {
332336
"text/plain": [
333-
"3.3333333333333335"
337+
"(3.0, 1.0)"
334338
]
335339
},
336-
"execution_count": 15,
340+
"execution_count": 18,
337341
"metadata": {},
338342
"output_type": "execute_result"
339343
}
340344
],
341345
"source": [
342-
"10 / float(3)"
346+
"divmod(10, float(3))"
343347
]
344348
},
345349
{
@@ -1341,7 +1345,7 @@
13411345
"display_name": "Types"
13421346
},
13431347
"kernelspec": {
1344-
"display_name": "Python 3",
1348+
"display_name": "Python 3.9.13 ('rsd-course')",
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)