Skip to content

Commit 009da72

Browse files
committed
Replace sets with tuples
1 parent 99d1798 commit 009da72

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

part-4.ipynb

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:c41c7b2462d1eccc5d63748331ca0f9ee5635e45f1f51c325d9a923cc48cc7ad"
4+
"signature": "sha256:640caa4f1507ed924c3e826c02125468a1dc8314809695aa3b1b04bcae642559"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -514,19 +514,38 @@
514514
"cell_type": "markdown",
515515
"metadata": {},
516516
"source": [
517-
"## Sets\n",
517+
"## Tuples\n",
518518
"\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",
520520
"\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:"
522522
]
523523
},
524524
{
525525
"cell_type": "code",
526526
"collapsed": false,
527527
"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)"
530549
],
531550
"language": "python",
532551
"metadata": {},
@@ -536,15 +555,14 @@
536555
"cell_type": "markdown",
537556
"metadata": {},
538557
"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:"
540559
]
541560
},
542561
{
543562
"cell_type": "code",
544563
"collapsed": false,
545564
"input": [
546-
"empty_dictionary = {}\n",
547-
"type(empty_dictionary)"
565+
"stuff = 1, 2, 3"
548566
],
549567
"language": "python",
550568
"metadata": {},
@@ -554,8 +572,7 @@
554572
"cell_type": "code",
555573
"collapsed": false,
556574
"input": [
557-
"empty_set = set()\n",
558-
"type(empty_set)"
575+
"stuff"
559576
],
560577
"language": "python",
561578
"metadata": {},
@@ -565,14 +582,14 @@
565582
"cell_type": "markdown",
566583
"metadata": {},
567584
"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:"
569586
]
570587
},
571588
{
572589
"cell_type": "code",
573590
"collapsed": false,
574591
"input": [
575-
"party_roster.add(\"Jeremy\")"
592+
"stuff[2] = 3"
576593
],
577594
"language": "python",
578595
"metadata": {},
@@ -582,24 +599,31 @@
582599
"cell_type": "markdown",
583600
"metadata": {},
584601
"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:"
586603
]
587604
},
588605
{
589606
"cell_type": "code",
590607
"collapsed": false,
591608
"input": [
592-
"\"Jessica\" in party_roster"
609+
"empty = ()"
593610
],
594611
"language": "python",
595612
"metadata": {},
596613
"outputs": []
597614
},
615+
{
616+
"cell_type": "markdown",
617+
"metadata": {},
618+
"source": [
619+
"Any guesses how we can make a single-item tuple?"
620+
]
621+
},
598622
{
599623
"cell_type": "code",
600624
"collapsed": false,
601625
"input": [
602-
"\"Bill\" in party_roster"
626+
"things = (3)"
603627
],
604628
"language": "python",
605629
"metadata": {},
@@ -609,25 +633,17 @@
609633
"cell_type": "code",
610634
"collapsed": false,
611635
"input": [
612-
"len(party_roster)"
636+
"things"
613637
],
614638
"language": "python",
615639
"metadata": {},
616640
"outputs": []
617641
},
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-
},
625642
{
626643
"cell_type": "code",
627644
"collapsed": false,
628645
"input": [
629-
"bringing_dish = {\"Jessica\", \"Jeremy\"}\n",
630-
"bringing_dessert = {\"Alice\", \"Jeremy\"}"
646+
"type(things)"
631647
],
632648
"language": "python",
633649
"metadata": {},
@@ -637,17 +653,14 @@
637653
"cell_type": "markdown",
638654
"metadata": {},
639655
"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:"
641657
]
642658
},
643659
{
644660
"cell_type": "code",
645661
"collapsed": false,
646662
"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,)"
651664
],
652665
"language": "python",
653666
"metadata": {},
@@ -657,10 +670,7 @@
657670
"cell_type": "code",
658671
"collapsed": false,
659672
"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"
664674
],
665675
"language": "python",
666676
"metadata": {},

0 commit comments

Comments
 (0)