|
66 | 66 | "collapsed": true
|
67 | 67 | },
|
68 | 68 | "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." |
70 | 70 | ]
|
71 | 71 | },
|
72 | 72 | {
|
|
142 | 142 | "cell_type": "markdown",
|
143 | 143 | "metadata": {},
|
144 | 144 | "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!" |
146 | 146 | ]
|
147 | 147 | },
|
148 | 148 | {
|
|
154 | 154 | "name": "stdout",
|
155 | 155 | "output_type": "stream",
|
156 | 156 | "text": [
|
157 |
| - "0\n" |
| 157 | + "3\n" |
158 | 158 | ]
|
159 | 159 | }
|
160 | 160 | ],
|
161 | 161 | "source": [
|
162 |
| - "print(one // ten)" |
| 162 | + "print(1 + 2) # returns an integer" |
163 | 163 | ]
|
164 | 164 | },
|
165 | 165 | {
|
|
168 | 168 | "metadata": {},
|
169 | 169 | "outputs": [
|
170 | 170 | {
|
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 | + ] |
179 | 176 | }
|
180 | 177 | ],
|
181 | 178 | "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." |
183 | 187 | ]
|
184 | 188 | },
|
185 | 189 | {
|
|
188 | 192 | "metadata": {},
|
189 | 193 | "outputs": [
|
190 | 194 | {
|
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" |
196 | 203 | }
|
197 | 204 | ],
|
198 | 205 | "source": [
|
199 |
| - "print(type(one / ten))" |
| 206 | + "10 / 3" |
200 | 207 | ]
|
201 | 208 | },
|
202 | 209 | {
|
|
207 | 214 | {
|
208 | 215 | "data": {
|
209 | 216 | "text/plain": [
|
210 |
| - "float" |
| 217 | + "3.3333333333333335" |
211 | 218 | ]
|
212 | 219 | },
|
213 | 220 | "execution_count": 10,
|
|
216 | 223 | }
|
217 | 224 | ],
|
218 | 225 | "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" |
228 | 227 | ]
|
229 | 228 | },
|
230 | 229 | {
|
|
235 | 234 | {
|
236 | 235 | "data": {
|
237 | 236 | "text/plain": [
|
238 |
| - "3" |
| 237 | + "3.3333333333333335" |
239 | 238 | ]
|
240 | 239 | },
|
241 | 240 | "execution_count": 11,
|
|
244 | 243 | }
|
245 | 244 | ],
|
246 | 245 | "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." |
248 | 254 | ]
|
249 | 255 | },
|
250 | 256 | {
|
|
253 | 259 | "metadata": {},
|
254 | 260 | "outputs": [
|
255 | 261 | {
|
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 | + ] |
264 | 267 | }
|
265 | 268 | ],
|
266 | 269 | "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." |
268 | 279 | ]
|
269 | 280 | },
|
270 | 281 | {
|
|
275 | 286 | {
|
276 | 287 | "data": {
|
277 | 288 | "text/plain": [
|
278 |
| - "3.3333333333333335" |
| 289 | + "(3.0, 1.0)" |
279 | 290 | ]
|
280 | 291 | },
|
281 | 292 | "execution_count": 13,
|
|
284 | 295 | }
|
285 | 296 | ],
|
286 | 297 | "source": [
|
287 |
| - "10 / 3.0" |
| 298 | + "divmod(10, 3.0)" |
288 | 299 | ]
|
289 | 300 | },
|
290 | 301 | {
|
291 | 302 | "cell_type": "markdown",
|
292 | 303 | "metadata": {},
|
293 | 304 | "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." |
302 | 306 | ]
|
303 | 307 | },
|
304 | 308 | {
|
|
330 | 334 | {
|
331 | 335 | "data": {
|
332 | 336 | "text/plain": [
|
333 |
| - "3.3333333333333335" |
| 337 | + "(3.0, 1.0)" |
334 | 338 | ]
|
335 | 339 | },
|
336 | 340 | "execution_count": 15,
|
|
339 | 343 | }
|
340 | 344 | ],
|
341 | 345 | "source": [
|
342 |
| - "10 / float(3)" |
| 346 | + "divmod(10, float(3))" |
343 | 347 | ]
|
344 | 348 | },
|
345 | 349 | {
|
|
1241 | 1245 | "traceback": [
|
1242 | 1246 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
1243 | 1247 | "\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", |
1245 | 1249 | "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 4)"
|
1246 | 1250 | ]
|
1247 | 1251 | }
|
|
1262 | 1266 | "traceback": [
|
1263 | 1267 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
1264 | 1268 | "\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", |
1266 | 1270 | "\u001b[0;31mValueError\u001b[0m: not enough values to unpack (expected 4, got 2)"
|
1267 | 1271 | ]
|
1268 | 1272 | }
|
|
1341 | 1345 | "display_name": "Types"
|
1342 | 1346 | },
|
1343 | 1347 | "kernelspec": {
|
1344 |
| - "display_name": "Python 3", |
| 1348 | + "display_name": "Python 3 (ipykernel)", |
1345 | 1349 | "language": "python",
|
1346 | 1350 | "name": "python3"
|
1347 | 1351 | },
|
|
1355 | 1359 | "name": "python",
|
1356 | 1360 | "nbconvert_exporter": "python",
|
1357 | 1361 | "pygments_lexer": "ipython3",
|
1358 |
| - "version": "3.7.3" |
| 1362 | + "version": "3.9.13" |
| 1363 | + }, |
| 1364 | + "vscode": { |
| 1365 | + "interpreter": { |
| 1366 | + "hash": "56f3b33ce8ef81dba99c545090023eafaf7aedb33c344d352a9ab6fb4e2c3676" |
| 1367 | + } |
1359 | 1368 | }
|
1360 | 1369 | },
|
1361 | 1370 | "nbformat": 4,
|
|
0 commit comments