From fea443d75bb9c04b09ff6d4a2c9e83a34daab0c6 Mon Sep 17 00:00:00 2001 From: tkoar Date: Tue, 13 Mar 2018 16:50:10 -0400 Subject: [PATCH 1/2] added README and re-ran the index.ipynb file to re-assign variables and show the correct string interpolation in example --- README.md | 302 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.ipynb | 94 ++++++++-------- 2 files changed, 347 insertions(+), 49 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000000..bc152e3e9e --- /dev/null +++ b/README.md @@ -0,0 +1,302 @@ + +# Naming things with Variables + +> "There are only two hard things in Computer Science: cache invalidation and naming things." + +> -- Phil Karlton + +> "...But ordinary language is all right." + +> Ludwig Wittgenstein + +### Objectives + +* Learn about how to use variables to give meaning to data +* Learn how to assign a variable to data +* Learn how to declare a variable +* Learn how to reassign a variable + +### Declaring and Assigning Variables + +So far we have only worked with data. Strings, numbers, True and False. In this lesson, we lesson we learn how to use variables to assign names to this data. For example, this is a string from our Working with Data Types Lab. + + +```python +"art vandelay" +``` + + + + + 'art vandelay' + + + +Now months later, if we see that string in some code, we may be confused as to what it's about. And when we add more data, this only becomes more difficult. Think of the what we saw in our Data Types Lab: `"art.vandelay@vandelay.co"`, `"Ceo"`, `"7285553334"`, `"vandelay.com"`. There's a lot to keep track of. + +So let's use a variables to indicate what each of these strings mean. + + +```python +email = "art.vandelay@vandelay.co" +``` + +In programming terms, we say that just "declared a variable `email` and assigned it to the string `"art.vandelay@vandelay.co"`". And to do so, we'll follow the procedure above: + +`variable = data` + +Now that we have assigned a variable `email` to a string, we just type the word `email` to see the string again. + + +```python +email +``` + + + + + 'art.vandelay@vandelay.co' + + + +Ok, let's do this with the website too. + + +```python +website = "vandelay.com" +website +``` + + + + + 'vandelay.com' + + + +Note that if you introduce a new variable, (declare it), but do not also assign it in the same line, Python will raise an error. + + +```python +name +``` + + + ---------------------------------------------------------- + + NameError Traceback (most recent call last) + + in () + ----> 1 name + + + NameError: name 'name' is not defined + + +So that error tells us that `name` is not defined. We just fix this by declaring `name` and assigning the variable in the same line. + + +```python +name = 'Art Vandalay' +name +``` + + + + + 'Art Vandalay' + + + +So this is assigning and reading a variable. And when we want to see some information again, we can easily find out. + + +```python +email +``` + + + + + 'art.vandelay@vandelay.co' + + + +### Reassigning variables + +Now that we have this data, you can imagine using it for some instructions. For example, say you want to write yourself - if you're fortunate, a direct report - some instructions on who and how to reach out to someone you just met. Here's the message: + + +```python +"Send an email to Art Vandalay at 'art.vandelay@vandelay.com' to tell say how nice it was meeting yesterday." +``` + + + + + "Send an email to Art Vandalay at 'art.vandelay@vandelay.com' to tell say how nice it was meeting yesterday." + + + +If we construct this message with variables, we have the following: + + +```python +"Send an email to " + name + " at " + email + " to say how nice it was meeting yesterday." +``` + + + + + 'Send an email to Art Vandalay at art.vandelay@vandelay.co to say how nice it was meeting yesterday.' + + + +Now, you meet someone else, "Liz Kaplan" with the email of "liz@ka-plan.com" and want to give the same instructions, but the only thing that varies are the name and email. So then this is easy enough. First we change what the data that the variable points to. + + +```python +name = 'Liz Kaplan' +email = 'liz@ka-plan.com' +``` + +So as you can see, we reassign our variable by just setting `variable = 'new data'`. And our variable is then updated. + + +```python +name # 'Liz Kaplan' +``` + + + + + 'Liz Kaplan' + + + + +```python +email # 'liz@ka-plan.com' +``` + + + + + 'liz@ka-plan.com' + + + +And now, our previous message is automatically updated. + + +```python +"Send an email to " + name + " at " + email + " to tell him how nice it was meeting him yesterday." +``` + + + + + 'Send an email to Liz Kaplan at liz@ka-plan.com to tell him how nice it was meeting him yesterday.' + + + +So in the line above, we are getting to some of the real power of programming. By choosing the correct variable name, we can begin to say treat this data like a `name` or an `email`, regardless of what the underlying value is. And then we can perform easily perform the same operation on different underlying values. + +### Operating on variables + +Just to hammer this point home let's see what we can now do with the name variable. + + +```python +name +``` + + + + + 'Liz Kaplan' + + + + +```python +name.upper() +``` + + + + + 'LIZ KAPLAN' + + + + +```python +name.title() +``` + + + + + 'Liz Kaplan' + + + +So just like we directly call methods on a string, we can also call methods on a variable that points to a string. And, if try to call a methods on something that you think is a string, but really is a number, you will see an error. + + +```python +name = 42 +``` + + +```python +name.upper() +``` + + + ---------------------------------------------------------- + + AttributeError Traceback (most recent call last) + + in () + ----> 1 name.upper() + + + AttributeError: 'int' object has no attribute 'upper' + + +Just like we would recieve that error from calling on number `42` more easily. So now, that we are working with variables, you may run into errors where you think a variable is one thing, but really it is something else. But it's no big deal. We just see what the variable is. + + +```python +name +``` + + + + + 42 + + + +And make the change. + + +```python +name = 'Liz Kaplan' +name +``` + + + + + 'Liz Kaplan' + + + +### Summary + +In this lesson, we got a taste for what makes computer programs so powerful. By using variables, we can write programs that know how to combine data. This can save us time by avoiding boring, repetitive tasks. We declare and assign a variable with the pattern of `variable = data`. And reassign a variable with the same pattern. To refernece a variable, we simply type the variable's name. + +We also saw that one of the things to pay attention to when working with variables is that they are sometimes different from what we expect. So we just type the name of the variable, to see what it really is and make the change. diff --git a/index.ipynb b/index.ipynb index 6f116a0634..c5eeeb3a6d 100644 --- a/index.ipynb +++ b/index.ipynb @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -62,7 +62,7 @@ "'art vandelay'" ] }, - "execution_count": 8, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -82,10 +82,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "email = \"art.vandelay@vandelay.co\"" @@ -104,7 +102,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -113,7 +111,7 @@ "'art.vandelay@vandelay.co'" ] }, - "execution_count": 10, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -131,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -140,7 +138,7 @@ "'vandelay.com'" ] }, - "execution_count": 11, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -159,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -167,9 +165,9 @@ "evalue": "name 'name' is not defined", "output_type": "error", "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31m----------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mname\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'name' is not defined" ] } @@ -187,7 +185,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -196,7 +194,7 @@ "'Art Vandalay'" ] }, - "execution_count": 14, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -215,7 +213,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -224,7 +222,7 @@ "'art.vandelay@vandelay.co'" ] }, - "execution_count": 15, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -249,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -258,7 +256,7 @@ "\"Send an email to Art Vandalay at 'art.vandelay@vandelay.com' to tell say how nice it was meeting yesterday.\"" ] }, - "execution_count": 26, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -276,16 +274,16 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'Send an email to Liz Kaplan at liz@ka-plan.com to say how nice it was meeting yesterday.'" + "'Send an email to Art Vandalay at art.vandelay@vandelay.co to say how nice it was meeting yesterday.'" ] }, - "execution_count": 27, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -303,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -320,7 +318,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -329,7 +327,7 @@ "'Liz Kaplan'" ] }, - "execution_count": 33, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -340,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -349,7 +347,7 @@ "'liz@ka-plan.com'" ] }, - "execution_count": 35, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -367,7 +365,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -376,7 +374,7 @@ "'Send an email to Liz Kaplan at liz@ka-plan.com to tell him how nice it was meeting him yesterday.'" ] }, - "execution_count": 36, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -408,7 +406,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -417,7 +415,7 @@ "'Liz Kaplan'" ] }, - "execution_count": 37, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -428,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -437,7 +435,7 @@ "'LIZ KAPLAN'" ] }, - "execution_count": 38, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } @@ -448,7 +446,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -457,7 +455,7 @@ "'Liz Kaplan'" ] }, - "execution_count": 39, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -475,10 +473,8 @@ }, { "cell_type": "code", - "execution_count": 42, - "metadata": { - "collapsed": true - }, + "execution_count": 18, + "metadata": {}, "outputs": [], "source": [ "name = 42" @@ -486,7 +482,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -494,9 +490,9 @@ "evalue": "'int' object has no attribute 'upper'", "output_type": "error", "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mname\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31m----------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mname\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'int' object has no attribute 'upper'" ] } @@ -514,7 +510,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -523,7 +519,7 @@ "42" ] }, - "execution_count": 44, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -541,7 +537,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -550,7 +546,7 @@ "'Liz Kaplan'" ] }, - "execution_count": 47, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -593,7 +589,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.6.4" } }, "nbformat": 4, From 737dd11a7297c0d4a4c063ec26464ca84e0959e1 Mon Sep 17 00:00:00 2001 From: Jeffrey Katz Date: Tue, 27 Mar 2018 12:49:37 -0400 Subject: [PATCH 2/2] small revisions --- index.ipynb | 227 ++++++++++++---------------------------------------- 1 file changed, 52 insertions(+), 175 deletions(-) diff --git a/index.ipynb b/index.ipynb index c5eeeb3a6d..2d505a92a1 100644 --- a/index.ipynb +++ b/index.ipynb @@ -83,12 +83,21 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "email = \"art.vandelay@vandelay.co\"" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> For this, and all of the subsequent code in gray boxes, you should press shift + enter to ensure that the code executes. If you do not do so with the line above for example, then email when we reference email in the lines that follow, Jupyter will throw an error indicating that the variable is undefined. So it is not enough to just type the correct code, we need to run shift + enter on our gray boxes to run this code." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -102,24 +111,20 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'art.vandelay@vandelay.co'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "email" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> Press shift + enter on the gray box above to see what `email` equals." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -129,20 +134,9 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'vandelay.com'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "website = \"vandelay.com\"\n", "website" @@ -185,20 +179,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Art Vandalay'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name = 'Art Vandalay'\n", "name" @@ -228,7 +211,7 @@ } ], "source": [ - "email" + "email\n" ] }, { @@ -247,20 +230,9 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"Send an email to Art Vandalay at 'art.vandelay@vandelay.com' to tell say how nice it was meeting yesterday.\"" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Send an email to Art Vandalay at 'art.vandelay@vandelay.com' to tell say how nice it was meeting yesterday.\"" ] @@ -274,20 +246,9 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Send an email to Art Vandalay at art.vandelay@vandelay.co to say how nice it was meeting yesterday.'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Send an email to \" + name + \" at \" + email + \" to say how nice it was meeting yesterday.\"" ] @@ -301,8 +262,10 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": {}, + "execution_count": null, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "name = 'Liz Kaplan'\n", @@ -318,40 +281,18 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Liz Kaplan'" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name # 'Liz Kaplan'" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'liz@ka-plan.com'" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "email # 'liz@ka-plan.com'" ] @@ -365,20 +306,9 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Send an email to Liz Kaplan at liz@ka-plan.com to tell him how nice it was meeting him yesterday.'" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\"Send an email to \" + name + \" at \" + email + \" to tell him how nice it was meeting him yesterday.\"" ] @@ -406,60 +336,27 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Liz Kaplan'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'LIZ KAPLAN'" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name.upper()" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Liz Kaplan'" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name.title()" ] @@ -474,7 +371,9 @@ { "cell_type": "code", "execution_count": 18, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "name = 42" @@ -510,20 +409,9 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "42" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name" ] @@ -537,20 +425,9 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Liz Kaplan'" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "name = 'Liz Kaplan'\n", "name" @@ -589,7 +466,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.4" + "version": "3.6.1" } }, "nbformat": 4,