|
12 | 12 | "\n",
|
13 | 13 | "Key concepts\n",
|
14 | 14 | "* Primitive values and types: `int`, `float`, `complex`, `bool`, `str`, and `NoneType`\n",
|
15 |
| - "* Type queries\n", |
16 |
| - "* Basic string operations\n", |
17 |
| - "* Slicing and `slice` objects" |
| 15 | + " - Type promotion\n", |
| 16 | + " - Type queries\n", |
| 17 | + "* Strings\n", |
| 18 | + " - Indexing\n", |
| 19 | + " - Slicing and `slice` objects\n", |
| 20 | + "* Booleans and bit manipulation\n", |
| 21 | + "* Syntatic sugar: Update operations" |
18 | 22 | ]
|
19 | 23 | },
|
20 | 24 | {
|
|
172 | 176 | "print(type(5.0), isinstance(5.0, complex)) # Does not always behave as expected"
|
173 | 177 | ]
|
174 | 178 | },
|
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "(5.0).is_integer() # Special test for floating-point values" |
| 186 | + ] |
| 187 | + }, |
175 | 188 | {
|
176 | 189 | "cell_type": "markdown",
|
177 | 190 | "metadata": {},
|
|
191 | 204 | "print(x is None, x is not None) # Preferred idiom"
|
192 | 205 | ]
|
193 | 206 | },
|
| 207 | + { |
| 208 | + "cell_type": "markdown", |
| 209 | + "metadata": {}, |
| 210 | + "source": [ |
| 211 | + "**\"Big\" integers by default.** Integers do not \"overflow\" as they do in most other languages." |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": null, |
| 217 | + "metadata": {}, |
| 218 | + "outputs": [], |
| 219 | + "source": [ |
| 220 | + "print('(a)', 3**4)\n", |
| 221 | + "print('(b)', 3**40)\n", |
| 222 | + "print('(c)', 3**400)" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "markdown", |
| 227 | + "metadata": {}, |
| 228 | + "source": [ |
| 229 | + "**Math functions.** For numerical values, many of the functions you might want are available in the `math` module." |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "code", |
| 234 | + "execution_count": null, |
| 235 | + "metadata": {}, |
| 236 | + "outputs": [], |
| 237 | + "source": [ |
| 238 | + "import math\n", |
| 239 | + "\n", |
| 240 | + "math" |
| 241 | + ] |
| 242 | + }, |
194 | 243 | {
|
195 | 244 | "cell_type": "markdown",
|
196 | 245 | "metadata": {},
|
|
392 | 441 | "cell_type": "markdown",
|
393 | 442 | "metadata": {},
|
394 | 443 | "source": [
|
395 |
| - "## Boolean (logical) values and operations\n", |
| 444 | + "## Booleans and bit manipulation\n", |
| 445 | + "\n", |
| 446 | + "Boolean variables can take on the values of `True` or `False`. The built-in boolean operations are `and`, `or`, and `not`." |
| 447 | + ] |
| 448 | + }, |
| 449 | + { |
| 450 | + "cell_type": "code", |
| 451 | + "execution_count": null, |
| 452 | + "metadata": {}, |
| 453 | + "outputs": [], |
| 454 | + "source": [ |
| 455 | + "print(True and False)\n", |
| 456 | + "print(True or True)\n", |
| 457 | + "print(not True)" |
| 458 | + ] |
| 459 | + }, |
| 460 | + { |
| 461 | + "cell_type": "markdown", |
| 462 | + "metadata": {}, |
| 463 | + "source": [ |
| 464 | + "In addition to booleans, you can also perform bit-level manipulations on integers. The following operators perform logical operations bitwise between the corresponding bits of the operands.\n", |
| 465 | + "\n", |
396 | 466 | "\n",
|
397 | 467 | "| Operator | Name | Description |\n",
|
398 | 468 | "|--------------|-----------------|---------------------------------------------|\n",
|
|
404 | 474 | "| ``~a`` | Bitwise NOT | Bitwise negation of ``a`` |"
|
405 | 475 | ]
|
406 | 476 | },
|
| 477 | + { |
| 478 | + "cell_type": "markdown", |
| 479 | + "metadata": {}, |
| 480 | + "source": [ |
| 481 | + "First, some different ways to inspect the binary representations of integer values as strings (see also [`bin()`](https://docs.python.org/3/library/functions.html#bin) in the Python docs):" |
| 482 | + ] |
| 483 | + }, |
| 484 | + { |
| 485 | + "cell_type": "code", |
| 486 | + "execution_count": null, |
| 487 | + "metadata": {}, |
| 488 | + "outputs": [], |
| 489 | + "source": [ |
| 490 | + "print(bin(5))\n", |
| 491 | + "print(format(5, '#b'))\n", |
| 492 | + "print(format(5, 'b')) # no prefix\n", |
| 493 | + "print(f'{5:#b}', f'{5:b}')\n", |
| 494 | + "print('{:07b}'.format(5)) # 7 bits with leading zeros" |
| 495 | + ] |
| 496 | + }, |
407 | 497 | {
|
408 | 498 | "cell_type": "code",
|
409 | 499 | "execution_count": null,
|
410 | 500 | "metadata": {},
|
411 | 501 | "outputs": [],
|
412 | 502 | "source": [
|
413 |
| - "print(True & False)\n", |
414 |
| - "print(True ^ True)" |
| 503 | + "print('{:06b}'.format(5)) # or use '{:#06b}' for a 6-bit string prefixed by '0b'\n", |
| 504 | + "print('{:06b}'.format(13))\n", |
| 505 | + "print('{:06b}'.format(5 & 13))" |
415 | 506 | ]
|
416 | 507 | },
|
417 | 508 | {
|
|
0 commit comments