|
504 | 504 | "print(s)" |
505 | 505 | ] |
506 | 506 | }, |
| 507 | + { |
| 508 | + "cell_type": "markdown", |
| 509 | + "metadata": {}, |
| 510 | + "source": [ |
| 511 | + "Die \"umgekehrte\" Operation geht natürlich auch, aber wir können nicht einfach jeden String in einen Integer umwandeln:" |
| 512 | + ] |
| 513 | + }, |
| 514 | + { |
| 515 | + "cell_type": "code", |
| 516 | + "execution_count": 6, |
| 517 | + "metadata": {}, |
| 518 | + "outputs": [ |
| 519 | + { |
| 520 | + "ename": "ValueError", |
| 521 | + "evalue": "invalid literal for int() with base 10: 'abcdef'", |
| 522 | + "output_type": "error", |
| 523 | + "traceback": [ |
| 524 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 525 | + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", |
| 526 | + "\u001b[0;32m/tmp/ipykernel_287561/681081256.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"5\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"abcdef\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 527 | + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'abcdef'" |
| 528 | + ] |
| 529 | + } |
| 530 | + ], |
| 531 | + "source": [ |
| 532 | + "x = int(\"5\")\n", |
| 533 | + "int(\"abcdef\")" |
| 534 | + ] |
| 535 | + }, |
| 536 | + { |
| 537 | + "cell_type": "markdown", |
| 538 | + "metadata": {}, |
| 539 | + "source": [ |
| 540 | + "Die Fehlermeldung weist uns auf ein ganz spannendes Feature hin: Wir können die Basis angeben.\n", |
| 541 | + "Bisher haben wir (standardmäßig) Dezimalzahlen verwendet (Basis 10). Wir können aber z.B. auch Hexadezimalzahlen (Basis 16) verwenden:" |
| 542 | + ] |
| 543 | + }, |
| 544 | + { |
| 545 | + "cell_type": "code", |
| 546 | + "execution_count": 9, |
| 547 | + "metadata": {}, |
| 548 | + "outputs": [ |
| 549 | + { |
| 550 | + "name": "stdout", |
| 551 | + "output_type": "stream", |
| 552 | + "text": [ |
| 553 | + "11259375\n", |
| 554 | + "0xabcdef\n" |
| 555 | + ] |
| 556 | + } |
| 557 | + ], |
| 558 | + "source": [ |
| 559 | + "x = int(\"abcdef\", 16)\n", |
| 560 | + "print(x)\n", |
| 561 | + "print(hex(x))" |
| 562 | + ] |
| 563 | + }, |
| 564 | + { |
| 565 | + "cell_type": "markdown", |
| 566 | + "metadata": {}, |
| 567 | + "source": [ |
| 568 | + "Analog zu `hex` existieren auch `bin` (binär, Basis 2) und `oct` (oktal, Basis 8).\n", |
| 569 | + "Diese drei sind so häufig, dass sich auch Literale damit formulieren lassen (`0x` für hexadezimal, `0b` für binär, `0o` für oktal):" |
| 570 | + ] |
| 571 | + }, |
| 572 | + { |
| 573 | + "cell_type": "code", |
| 574 | + "execution_count": 12, |
| 575 | + "metadata": {}, |
| 576 | + "outputs": [ |
| 577 | + { |
| 578 | + "name": "stdout", |
| 579 | + "output_type": "stream", |
| 580 | + "text": [ |
| 581 | + "0o777\n", |
| 582 | + "0b1011\n", |
| 583 | + "0xc0ffee\n" |
| 584 | + ] |
| 585 | + } |
| 586 | + ], |
| 587 | + "source": [ |
| 588 | + "print(oct(0o777))\n", |
| 589 | + "print(bin(0b1011))\n", |
| 590 | + "print(hex(0xc0ffee))" |
| 591 | + ] |
| 592 | + }, |
507 | 593 | { |
508 | 594 | "cell_type": "markdown", |
509 | 595 | "metadata": {}, |
|
680 | 766 | "source": [ |
681 | 767 | "import this" |
682 | 768 | ] |
683 | | - }, |
684 | | - { |
685 | | - "cell_type": "code", |
686 | | - "execution_count": null, |
687 | | - "metadata": {}, |
688 | | - "outputs": [], |
689 | | - "source": [] |
690 | 769 | } |
691 | 770 | ], |
692 | 771 | "metadata": { |
|
0 commit comments