|
1 | 1 | { |
2 | 2 | "metadata": { |
3 | 3 | "name": "", |
4 | | - "signature": "sha256:c41c7b2462d1eccc5d63748331ca0f9ee5635e45f1f51c325d9a923cc48cc7ad" |
| 4 | + "signature": "sha256:640caa4f1507ed924c3e826c02125468a1dc8314809695aa3b1b04bcae642559" |
5 | 5 | }, |
6 | 6 | "nbformat": 3, |
7 | 7 | "nbformat_minor": 0, |
|
514 | 514 | "cell_type": "markdown", |
515 | 515 | "metadata": {}, |
516 | 516 | "source": [ |
517 | | - "## Sets\n", |
| 517 | + "## Tuples\n", |
518 | 518 | "\n", |
519 | | - "Sets are unordered collections of distinct items. Lists can have duplicates of the same item, but sets only store one copy.\n", |
| 519 | + "Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n", |
520 | 520 | "\n", |
521 | | - "Let's say we want to make a roster of people who have RSVPed to our birthday party. If someone RSVPs twice we don't want them to show up in our roster twice. Let's use a set!" |
| 521 | + "Tuples are comma-separated lists:" |
522 | 522 | ] |
523 | 523 | }, |
524 | 524 | { |
525 | 525 | "cell_type": "code", |
526 | 526 | "collapsed": false, |
527 | 527 | "input": [ |
528 | | - "party_roster = {\"Jessica\", \"Tom\", \"Alice\"}\n", |
529 | | - "party_roster" |
| 528 | + "stuff = (1, 2, \"hello\")" |
| 529 | + ], |
| 530 | + "language": "python", |
| 531 | + "metadata": {}, |
| 532 | + "outputs": [] |
| 533 | + }, |
| 534 | + { |
| 535 | + "cell_type": "code", |
| 536 | + "collapsed": false, |
| 537 | + "input": [ |
| 538 | + "stuff" |
| 539 | + ], |
| 540 | + "language": "python", |
| 541 | + "metadata": {}, |
| 542 | + "outputs": [] |
| 543 | + }, |
| 544 | + { |
| 545 | + "cell_type": "code", |
| 546 | + "collapsed": false, |
| 547 | + "input": [ |
| 548 | + "type(stuff)" |
530 | 549 | ], |
531 | 550 | "language": "python", |
532 | 551 | "metadata": {}, |
|
536 | 555 | "cell_type": "markdown", |
537 | 556 | "metadata": {}, |
538 | 557 | "source": [ |
539 | | - "Notice how we used curly braces around our set (just like we did for dictionaries)? What happens if we want to represent an empty set?" |
| 558 | + "The parenthesis are often optional. They're often added for clarity. This should work too:" |
540 | 559 | ] |
541 | 560 | }, |
542 | 561 | { |
543 | 562 | "cell_type": "code", |
544 | 563 | "collapsed": false, |
545 | 564 | "input": [ |
546 | | - "empty_dictionary = {}\n", |
547 | | - "type(empty_dictionary)" |
| 565 | + "stuff = 1, 2, 3" |
548 | 566 | ], |
549 | 567 | "language": "python", |
550 | 568 | "metadata": {}, |
|
554 | 572 | "cell_type": "code", |
555 | 573 | "collapsed": false, |
556 | 574 | "input": [ |
557 | | - "empty_set = set()\n", |
558 | | - "type(empty_set)" |
| 575 | + "stuff" |
559 | 576 | ], |
560 | 577 | "language": "python", |
561 | 578 | "metadata": {}, |
|
565 | 582 | "cell_type": "markdown", |
566 | 583 | "metadata": {}, |
567 | 584 | "source": [ |
568 | | - "Our friend Jeremy just RSVPed, let's add him to our set too:" |
| 585 | + "Let's see what happens when we try to change our tuple:" |
569 | 586 | ] |
570 | 587 | }, |
571 | 588 | { |
572 | 589 | "cell_type": "code", |
573 | 590 | "collapsed": false, |
574 | 591 | "input": [ |
575 | | - "party_roster.add(\"Jeremy\")" |
| 592 | + "stuff[2] = 3" |
576 | 593 | ], |
577 | 594 | "language": "python", |
578 | 595 | "metadata": {}, |
|
582 | 599 | "cell_type": "markdown", |
583 | 600 | "metadata": {}, |
584 | 601 | "source": [ |
585 | | - "We can check for size and membership of sets and loop over them just like with sets, strings, and dictionaries." |
| 602 | + "We can make an empty tuple with an empty set of parenthesis:" |
586 | 603 | ] |
587 | 604 | }, |
588 | 605 | { |
589 | 606 | "cell_type": "code", |
590 | 607 | "collapsed": false, |
591 | 608 | "input": [ |
592 | | - "\"Jessica\" in party_roster" |
| 609 | + "empty = ()" |
593 | 610 | ], |
594 | 611 | "language": "python", |
595 | 612 | "metadata": {}, |
596 | 613 | "outputs": [] |
597 | 614 | }, |
| 615 | + { |
| 616 | + "cell_type": "markdown", |
| 617 | + "metadata": {}, |
| 618 | + "source": [ |
| 619 | + "Any guesses how we can make a single-item tuple?" |
| 620 | + ] |
| 621 | + }, |
598 | 622 | { |
599 | 623 | "cell_type": "code", |
600 | 624 | "collapsed": false, |
601 | 625 | "input": [ |
602 | | - "\"Bill\" in party_roster" |
| 626 | + "things = (3)" |
603 | 627 | ], |
604 | 628 | "language": "python", |
605 | 629 | "metadata": {}, |
|
609 | 633 | "cell_type": "code", |
610 | 634 | "collapsed": false, |
611 | 635 | "input": [ |
612 | | - "len(party_roster)" |
| 636 | + "things" |
613 | 637 | ], |
614 | 638 | "language": "python", |
615 | 639 | "metadata": {}, |
616 | 640 | "outputs": [] |
617 | 641 | }, |
618 | | - { |
619 | | - "cell_type": "markdown", |
620 | | - "metadata": {}, |
621 | | - "source": [ |
622 | | - "Our party is a potluck. Everyone is supposed to bring a dinner dish or a dessert. Let's make sets for everyone bringing a dish and everyone bringing a dessert." |
623 | | - ] |
624 | | - }, |
625 | 642 | { |
626 | 643 | "cell_type": "code", |
627 | 644 | "collapsed": false, |
628 | 645 | "input": [ |
629 | | - "bringing_dish = {\"Jessica\", \"Jeremy\"}\n", |
630 | | - "bringing_dessert = {\"Alice\", \"Jeremy\"}" |
| 646 | + "type(things)" |
631 | 647 | ], |
632 | 648 | "language": "python", |
633 | 649 | "metadata": {}, |
|
637 | 653 | "cell_type": "markdown", |
638 | 654 | "metadata": {}, |
639 | 655 | "source": [ |
640 | | - "Sets allow us to use set operations from math to find the union, intersection, difference, and symmetric difference between sets. Let's try it out:" |
| 656 | + "Python just sees this as a number with parenthesis around it. The commas are the important part. We need to add a comma after the number to make a single-item tuple:" |
641 | 657 | ] |
642 | 658 | }, |
643 | 659 | { |
644 | 660 | "cell_type": "code", |
645 | 661 | "collapsed": false, |
646 | 662 | "input": [ |
647 | | - "bringing_dish_or_dessert = bringing_dish.union(bringing_dessert)\n", |
648 | | - "bringing_dish_and_dessert = bringing_dish.intersection(bringing_dessert)\n", |
649 | | - "bringing_just_one_item = bringing_dish.symmetric_difference(bringing_dessert)\n", |
650 | | - "not_bringing_food = party_roster.difference(bringing_dish_or_dessert)" |
| 663 | + "things = (3,)" |
651 | 664 | ], |
652 | 665 | "language": "python", |
653 | 666 | "metadata": {}, |
|
657 | 670 | "cell_type": "code", |
658 | 671 | "collapsed": false, |
659 | 672 | "input": [ |
660 | | - "print(bringing_dish_or_dessert)\n", |
661 | | - "print(bringing_dish_and_dessert)\n", |
662 | | - "print(bringing_just_one_item)\n", |
663 | | - "print(not_bringing_food)" |
| 673 | + "things" |
664 | 674 | ], |
665 | 675 | "language": "python", |
666 | 676 | "metadata": {}, |
|
0 commit comments