Skip to content

Adds detail to tuple section #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 137 additions & 12 deletions part-4.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:640caa4f1507ed924c3e826c02125468a1dc8314809695aa3b1b04bcae642559"
"signature": "sha256:7b5c09563e39ddf2ede88b8ff357dcdc654af16292c9c86e3bca2fbf703f64c2"
},
"nbformat": 3,
"nbformat_minor": 0,
Expand Down Expand Up @@ -518,24 +518,33 @@
"\n",
"Tuples are similar to lists except they're immutable, which means we can't change them after they've been made.\n",
"\n",
"One real world use of tuples is to hold one particular record within a complex file. For example, a stock holding or a restaurant inventory.\n",
"\n",
"Tuples are comma-separated lists:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"stuff = (1, 2, \"hello\")"
"food = (\"cones\", 100, 3.79)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is sometimes called packing a tuple."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"stuff"
"food"
],
"language": "python",
"metadata": {},
Expand All @@ -545,7 +554,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"type(stuff)"
"type(food)"
],
"language": "python",
"metadata": {},
Expand All @@ -562,7 +571,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"stuff = 1, 2, 3"
"food = \"sprinkles\", 10, 4.99"
],
"language": "python",
"metadata": {},
Expand All @@ -572,7 +581,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"stuff"
"food"
],
"language": "python",
"metadata": {},
Expand All @@ -589,7 +598,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"stuff[2] = 3"
"food[2] = 3"
],
"language": "python",
"metadata": {},
Expand All @@ -612,6 +621,26 @@
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"empty"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(empty)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -623,7 +652,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"things = (3)"
"food = (3)"
],
"language": "python",
"metadata": {},
Expand All @@ -633,7 +662,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"things"
"food"
],
"language": "python",
"metadata": {},
Expand All @@ -643,7 +672,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"type(things)"
"type(food)"
],
"language": "python",
"metadata": {},
Expand All @@ -660,7 +689,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"things = (3,)"
"food = (3,)"
],
"language": "python",
"metadata": {},
Expand All @@ -670,11 +699,107 @@
"cell_type": "code",
"collapsed": false,
"input": [
"things"
"food"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(food)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes, unpacking a tuple is also desired."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"yummies = [(\"cones\", 100, 3.79), (\"sprinkles\", 10, 4.99), (\"hot fudge\", 8, 3.29), (\"nuts\", 6, 8.99)]"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"yummies[2]"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"name, amount, price = yummies[2]"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"Our ice cream shop serves \" + name + \".\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"The shop's inventory value for \" + name + \" is $\" + str(amount * price) + \".\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"All this food talk is making me hungry for \" + yummies[3][0] + \".\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Why not always use lists instead of tuples? Tuples use less storage space than a list. So if you are reading 1000s of small records from a file, a tuple can be an efficient way to go.\n",
"\n",
"A practical learning tip: When learning a new language, there are many new terms and concepts. Sometimes it is helpful to see a bigger picture of how everything comes together. I'm a lover of \"Table of Contents\" since I can scan the big picture and see where something like a tuple fits in. The official Python tutorial has a very good [Table of Contents]( https://docs.python.org/3.4/tutorial/index.html), and I sometimes take 5 minutes to just click and explore something new to me.\n",
"\n",
"Wow! We've come a long way today. "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
Expand Down