|
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 | {
|
149 | 149 | "cell_type": "code",
|
150 |
| - "execution_count": 7, |
| 150 | + "execution_count": 1, |
151 | 151 | "metadata": {},
|
152 | 152 | "outputs": [
|
153 | 153 | {
|
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 | {
|
166 | 166 | "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, |
188 | 168 | "metadata": {},
|
189 | 169 | "outputs": [
|
190 | 170 | {
|
191 | 171 | "name": "stdout",
|
192 | 172 | "output_type": "stream",
|
193 | 173 | "text": [
|
194 |
| - "<class 'float'>\n" |
| 174 | + "3.0\n" |
195 | 175 | ]
|
196 | 176 | }
|
197 | 177 | ],
|
198 | 178 | "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" |
220 | 180 | ]
|
221 | 181 | },
|
222 | 182 | {
|
223 | 183 | "cell_type": "markdown",
|
224 | 184 | "metadata": {},
|
225 | 185 | "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." |
228 | 187 | ]
|
229 | 188 | },
|
230 | 189 | {
|
231 | 190 | "cell_type": "code",
|
232 |
| - "execution_count": 11, |
| 191 | + "execution_count": 4, |
233 | 192 | "metadata": {},
|
234 | 193 | "outputs": [
|
235 | 194 | {
|
236 | 195 | "data": {
|
237 | 196 | "text/plain": [
|
238 |
| - "3" |
| 197 | + "3.3333333333333335" |
239 | 198 | ]
|
240 | 199 | },
|
241 |
| - "execution_count": 11, |
| 200 | + "execution_count": 4, |
242 | 201 | "metadata": {},
|
243 | 202 | "output_type": "execute_result"
|
244 | 203 | }
|
245 | 204 | ],
|
246 | 205 | "source": [
|
247 |
| - "10 // 3" |
| 206 | + "10 / 3" |
248 | 207 | ]
|
249 | 208 | },
|
250 | 209 | {
|
|
291 | 250 | "cell_type": "markdown",
|
292 | 251 | "metadata": {},
|
293 | 252 | "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=}\")" |
295 | 272 | ]
|
296 | 273 | },
|
297 | 274 | {
|
298 | 275 | "cell_type": "markdown",
|
299 | 276 | "metadata": {},
|
300 | 277 | "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." |
302 | 279 | ]
|
303 | 280 | },
|
304 | 281 | {
|
|
309 | 286 | {
|
310 | 287 | "data": {
|
311 | 288 | "text/plain": [
|
312 |
| - "float" |
| 289 | + "(3.0, 1.0)" |
313 | 290 | ]
|
314 | 291 | },
|
315 | 292 | "execution_count": 14,
|
316 | 293 | "metadata": {},
|
317 | 294 | "output_type": "execute_result"
|
318 | 295 | }
|
319 | 296 | ],
|
| 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 | + ], |
320 | 324 | "source": [
|
321 | 325 | "x = float(5)\n",
|
322 | 326 | "type(x)"
|
323 | 327 | ]
|
324 | 328 | },
|
325 | 329 | {
|
326 | 330 | "cell_type": "code",
|
327 |
| - "execution_count": 15, |
| 331 | + "execution_count": 18, |
328 | 332 | "metadata": {},
|
329 | 333 | "outputs": [
|
330 | 334 | {
|
331 | 335 | "data": {
|
332 | 336 | "text/plain": [
|
333 |
| - "3.3333333333333335" |
| 337 | + "(3.0, 1.0)" |
334 | 338 | ]
|
335 | 339 | },
|
336 |
| - "execution_count": 15, |
| 340 | + "execution_count": 18, |
337 | 341 | "metadata": {},
|
338 | 342 | "output_type": "execute_result"
|
339 | 343 | }
|
340 | 344 | ],
|
341 | 345 | "source": [
|
342 |
| - "10 / float(3)" |
| 346 | + "divmod(10, float(3))" |
343 | 347 | ]
|
344 | 348 | },
|
345 | 349 | {
|
|
1341 | 1345 | "display_name": "Types"
|
1342 | 1346 | },
|
1343 | 1347 | "kernelspec": {
|
1344 |
| - "display_name": "Python 3", |
| 1348 | + "display_name": "Python 3.9.13 ('rsd-course')", |
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