├── .gitignore ├── .learn ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md └── index.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /.learn: -------------------------------------------------------------------------------- 1 | jupyter_notebook: true 2 | 3 | tags: 4 | - python 5 | - jupyter 6 | 7 | languages: 8 | - python 9 | 10 | resources: 0 11 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Learn.co Curriculum 2 | 3 | We're really exited that you're about to contribute to the [open curriculum](https://learn.co/content-license) on [Learn.co](https://learn.co). If this is your first time contributing, please continue reading to learn how to make the most meaningful and useful impact possible. 4 | 5 | ## Raising an Issue to Encourage a Contribution 6 | 7 | If you notice a problem with the curriculum that you believe needs improvement 8 | but you're unable to make the change yourself, you should raise a Github issue 9 | containing a clear description of the problem. Include relevant snippets of 10 | the content and/or screenshots if applicable. Curriculum owners regularly review 11 | issue lists and your issue will be prioritized and addressed as appropriate. 12 | 13 | ## Submitting a Pull Request to Suggest an Improvement 14 | 15 | If you see an opportunity for improvement and can make the change yourself go 16 | ahead and use a typical git workflow to make it happen: 17 | 18 | * Fork this curriculum repository 19 | * Make the change on your fork, with descriptive commits in the standard format 20 | * Open a Pull Request against this repo 21 | 22 | A curriculum owner will review your change and approve or comment on it in due 23 | course. 24 | 25 | # Why Contribute? 26 | 27 | Curriculum on Learn is publicly and freely available under Learn's 28 | [Educational Content License](https://learn.co/content-license). By 29 | embracing an open-source contribution model, our goal is for the curriculum 30 | on Learn to become, in time, the best educational content the world has 31 | ever seen. 32 | 33 | We need help from the community of Learners to maintain and improve the 34 | educational content. Everything from fixing typos, to correcting 35 | out-dated information, to improving exposition, to adding better examples, 36 | to fixing tests—all contributions to making the curriculum more effective are 37 | welcome. 38 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # Learn.co Educational Content License 2 | 3 | Copyright (c) 2018 Flatiron School, Inc 4 | 5 | The Flatiron School, Inc. owns this Educational Content. However, the Flatiron 6 | School supports the development and availability of educational materials in 7 | the public domain. Therefore, the Flatiron School grants Users of the Flatiron 8 | Educational Content set forth in this repository certain rights to reuse, build 9 | upon and share such Educational Content subject to the terms of the Educational 10 | Content License set forth [here](http://learn.co/content-license) 11 | (http://learn.co/content-license). You must read carefully the terms and 12 | conditions contained in the Educational Content License as such terms govern 13 | access to and use of the Educational Content. 14 | 15 | Flatiron School is willing to allow you access to and use of the Educational 16 | Content only on the condition that you accept all of the terms and conditions 17 | contained in the Educational Content License set forth 18 | [here](http://learn.co/content-license) (http://learn.co/content-license). By 19 | accessing and/or using the Educational Content, you are agreeing to all of the 20 | terms and conditions contained in the Educational Content License. If you do 21 | not agree to any or all of the terms of the Educational Content License, you 22 | are prohibited from accessing, reviewing or using in any way the Educational 23 | Content. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Naming things with Variables 3 | 4 | ### Introduction 5 | 6 | > "There are only two hard things in Computer Science: cache invalidation and naming things." 7 | 8 | > -- Phil Karlton 9 | 10 | > "...But ordinary language is all right." 11 | 12 | > Ludwig Wittgenstein 13 | 14 | ### Objectives 15 | 16 | * Learn about how to use variables to give meaning to data 17 | * Learn how to assign a variable to data 18 | * Learn how to declare a variable 19 | * Learn how to reassign a variable 20 | 21 | ### Declaring and Assigning Variables 22 | 23 | So far we have worked with data -- strings, numbers, and booleans. In this lesson, we'll learn how to use variables to assign names to this data. For example, this is a string from our Working with **Data Types Lab**. 24 | 25 | 26 | ```python 27 | "art vandelay" 28 | ``` 29 | 30 | 31 | 32 | 33 | 'art vandelay' 34 | 35 | 36 | 37 | Now months later, if we see that string in some code, we may be confused as to what it is, and with even more data, this only becomes more difficult. Think of what we saw in our **Data Types Lab**: `"art.vandelay@vandelay.co"`, `"Ceo"`, `"7285553334"`, `"vandelay.com"`. There's a lot to keep track of. 38 | 39 | So, let's use variables to indicate what each of these strings mean. 40 | 41 | 42 | ```python 43 | email = "art.vandelay@vandelay.co" 44 | ``` 45 | 46 | > **Note:** 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 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. 47 | 48 | In programming terms, we say that we just declared a variable, `email`, and assigned it to the string, `"art.vandelay@vandelay.co"`. To do so, we'll follow the procedure below: 49 | 50 | variable = data 51 | 52 | Now that we have assigned a variable `email` to a string, we just type the word `email` to see the string again. 53 | 54 | 55 | ```python 56 | email 57 | ``` 58 | 59 | > *remember to press shift + enter on the gray box above to see the value of our variable, *`email`*.* 60 | 61 | Now let's try this with the website: 62 | 63 | 64 | ```python 65 | website = "vandelay.com" 66 | website 67 | ``` 68 | 69 | 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. 70 | 71 | 72 | ```python 73 | name 74 | ``` 75 | 76 | 77 | ---------------------------------------------------------- 78 | 79 | NameError Traceback (most recent call last) 80 | 81 | in () 82 | ----> 1 name 83 | 84 | 85 | NameError: name 'name' is not defined 86 | 87 | 88 | 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. 89 | 90 | 91 | ```python 92 | name = 'Art Vandelay' 93 | name 94 | ``` 95 | 96 | So this is assigning and reading a variable. And when we want to see some information again, we can easily find out. 97 | 98 | 99 | ```python 100 | email 101 | ``` 102 | 103 | ### Declaring variables without assignment 104 | 105 | We have seen that we can have data without assigning it to variables. 106 | 107 | 108 | ```python 109 | "Unassigned data" 110 | ``` 111 | 112 | 113 | 114 | 115 | 'Unassigned data' 116 | 117 | 118 | 119 | Sometimes we wish to declare a variable without assigning it to data. In Python, that's a little tricky to do. As we just saw with `name`, declaring variables without assignment throws an error. Thankfully, Python has a special type for us that represents nothing at all. 120 | 121 | 122 | ```python 123 | None 124 | ``` 125 | 126 | 127 | ```python 128 | type(None) 129 | ``` 130 | 131 | 132 | 133 | 134 | NoneType 135 | 136 | 137 | 138 | None is a data type in Python that represents nothing. So, if we do not know the type of a variable and want to have the data to the variable be assigned later, we can assign that variable to `None`. 139 | 140 | 141 | ```python 142 | address = None 143 | ``` 144 | 145 | Notice that `address` is now assigned, but it is assigned to `None`. 146 | 147 | 148 | ```python 149 | address 150 | ``` 151 | 152 | **Note:** *when variables are assigned to `None`, pressing shift + enter on the cell block will not output anything.* 153 | 154 | ### Reassigning variables 155 | 156 | Now that we have this data, we can imagine using it for some kind of instruction. For example, say we want to write ourself a memo on how to reach out to someone we just met. Here's the message: 157 | 158 | 159 | ```python 160 | "Send an email to Art Vandelay at 'art.vandelay@vandelay.com' to say how nice it was meeting yesterday." 161 | ``` 162 | 163 | If we construct this message with variables, we can write the following: 164 | 165 | 166 | ```python 167 | name = "Art Vandelay" 168 | email = "art.vandelay@vandelay.com" 169 | ``` 170 | 171 | 172 | ```python 173 | "Send an email to " + name + " at " + email + " to say how nice it was meeting yesterday." 174 | ``` 175 | 176 | Now you meet someone else, "Liz Kaplan" with the email of "liz@ka-plan.com" and want to write a memo with the same instructions, but the only thing that varies are the name and email. This should be easy enough given the way we set up our memo above. First we need to change the variables, `name` and `email`, by setting them to our new data. 177 | 178 | 179 | ```python 180 | name = 'Liz Kaplan' 181 | email = 'liz@ka-plan.com' 182 | ``` 183 | 184 | So as you can see, we reassign our variables by just setting `variable = 'new data'`. Presto, our variable is then updated. 185 | 186 | 187 | ```python 188 | name # 'Liz Kaplan' 189 | ``` 190 | 191 | 192 | ```python 193 | email # 'liz@ka-plan.com' 194 | ``` 195 | 196 | Now, if we copy and re-run our previous code, we will see it is automatically updated. 197 | 198 | 199 | ```python 200 | "Send an email to " + name + " at " + email + " to say how nice it was meeting yesterday." 201 | ``` 202 | 203 | 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 change the values of `name` or `email` and operate on their underlying values in the same ways. 204 | 205 | ### Operating on variables 206 | 207 | Just to hammer this point home let's see what we can now do with the name variable. 208 | 209 | 210 | ```python 211 | name 212 | ``` 213 | 214 | 215 | ```python 216 | name.upper() 217 | ``` 218 | 219 | 220 | ```python 221 | name.title() 222 | ``` 223 | 224 | Just like how we are able to directly call methods on a string, we can also call methods on a variable that points to a string. And, if we try to call a method on something that we think is a string, but really is a number, we will see an error. 225 | 226 | 227 | ```python 228 | name = 42 229 | ``` 230 | 231 | 232 | ```python 233 | name.upper() 234 | ``` 235 | 236 | We receive the same error from calling `upper` directly on the number `42` as we do when we call `upper` on a variable that points to the number `42`. So, now that we are working with variables, we may run into errors where we thought a variable is one thing, but it is actually something else. Don't worry, this is no big deal. We can just check to see what the variable is. 237 | 238 | 239 | ```python 240 | name 241 | ``` 242 | 243 | Once we have see what the variable is, we can make our change. 244 | 245 | 246 | ```python 247 | name = 'Liz Kaplan' 248 | name 249 | ``` 250 | 251 | ### Summary 252 | 253 | 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 reference a variable, we simply type the variable's name. 254 | 255 | 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 any necessary changes. 256 | -------------------------------------------------------------------------------- /index.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Naming things with Variables" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Introduction" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "> \"There are only two hard things in Computer Science: cache invalidation and naming things.\"\n", 22 | "\n", 23 | "> -- Phil Karlton\n", 24 | "\n", 25 | "> \"...But ordinary language is all right.\" \n", 26 | "\n", 27 | "> Ludwig Wittgenstein" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "### Objectives" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "* Learn about how to use variables to give meaning to data\n", 42 | "* Learn how to assign a variable to data\n", 43 | "* Learn how to declare a variable\n", 44 | "* Learn how to reassign a variable" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "### Declaring and Assigning Variables" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "So far we have worked with data -- strings, numbers, and booleans. In this lesson, we'll learn how to use variables to assign names to this data. For example, this is a string from our Working with **Data Types Lab**." 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 7, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "data": { 68 | "text/plain": [ 69 | "'art vandelay'" 70 | ] 71 | }, 72 | "execution_count": 7, 73 | "metadata": {}, 74 | "output_type": "execute_result" 75 | } 76 | ], 77 | "source": [ 78 | "\"art vandelay\"" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "Now months later, if we see that string in some code, we may be confused as to what it is, and with even more data, this only becomes more difficult. Think of what we saw in our **Data Types Lab**: `\"art.vandelay@vandelay.co\"`, `\"Ceo\"`, `\"7285553334\"`, `\"vandelay.com\"`. There's a lot to keep track of.\n", 86 | "\n", 87 | "So, let's use variables to indicate what each of these strings mean." 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "email = \"art.vandelay@vandelay.co\"" 99 | ] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "metadata": {}, 104 | "source": [ 105 | "> **Note:** 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 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." 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "In programming terms, we say that we just declared a variable, `email`, and assigned it to the string, `\"art.vandelay@vandelay.co\"`. To do so, we'll follow the procedure below:\n", 113 | "\n", 114 | " variable = data\n", 115 | "\n", 116 | "Now that we have assigned a variable `email` to a string, we just type the word `email` to see the string again. " 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "email" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "> *remember to press shift + enter on the gray box above to see the value of our variable, *`email`*.*" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "Now let's try this with the website:" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "website = \"vandelay.com\"\n", 149 | "website" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "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." 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 6, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "ename": "NameError", 166 | "evalue": "name 'name' is not defined", 167 | "output_type": "error", 168 | "traceback": [ 169 | "\u001b[0;31m----------------------------------------------------------\u001b[0m", 170 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 171 | "\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", 172 | "\u001b[0;31mNameError\u001b[0m: name 'name' is not defined" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "name" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [ 184 | "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." 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": null, 190 | "metadata": { 191 | "collapsed": true 192 | }, 193 | "outputs": [], 194 | "source": [ 195 | "name = 'Art Vandelay'\n", 196 | "name" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "So this is assigning and reading a variable. And when we want to see some information again, we can easily find out." 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "email" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "### Declaring variables without assignment" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "metadata": {}, 225 | "source": [ 226 | "We have seen that we can have data without assigning it to variables. " 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 1, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "'Unassigned data'" 238 | ] 239 | }, 240 | "execution_count": 1, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "\"Unassigned data\"" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "Sometimes we wish to declare a variable without assigning it to data. In Python, that's a little tricky to do. As we just saw with `name`, declaring variables without assignment throws an error. Thankfully, Python has a special type for us that represents nothing at all." 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 2, 259 | "metadata": { 260 | "collapsed": true 261 | }, 262 | "outputs": [], 263 | "source": [ 264 | "None" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 5, 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "data": { 274 | "text/plain": [ 275 | "NoneType" 276 | ] 277 | }, 278 | "execution_count": 5, 279 | "metadata": {}, 280 | "output_type": "execute_result" 281 | } 282 | ], 283 | "source": [ 284 | "type(None)" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "None is a data type in Python that represents nothing. So, if we do not know the type of a variable and want to have the data to the variable be assigned later, we can assign that variable to `None`." 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 3, 297 | "metadata": { 298 | "collapsed": true 299 | }, 300 | "outputs": [], 301 | "source": [ 302 | "address = None" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "Notice that `address` is now assigned, but it is assigned to `None`." 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 6, 315 | "metadata": { 316 | "collapsed": true 317 | }, 318 | "outputs": [], 319 | "source": [ 320 | "address" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "**Note:** *when variables are assigned to `None`, pressing shift + enter on the cell block will not output anything.*" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "### Reassigning variables" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "Now that we have this data, we can imagine using it for some kind of instruction. For example, say we want to write ourself a memo on how to reach out to someone we just met. Here's the message:" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": null, 347 | "metadata": { 348 | "collapsed": true 349 | }, 350 | "outputs": [], 351 | "source": [ 352 | "\"Send an email to Art Vandelay at 'art.vandelay@vandelay.com' to say how nice it was meeting yesterday.\"" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "If we construct this message with variables, we can write the following:" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 9, 365 | "metadata": { 366 | "collapsed": true 367 | }, 368 | "outputs": [], 369 | "source": [ 370 | "name = \"Art Vandelay\"\n", 371 | "email = \"art.vandelay@vandelay.com\"" 372 | ] 373 | }, 374 | { 375 | "cell_type": "code", 376 | "execution_count": null, 377 | "metadata": {}, 378 | "outputs": [], 379 | "source": [ 380 | "\"Send an email to \" + name + \" at \" + email + \" to say how nice it was meeting yesterday.\"" 381 | ] 382 | }, 383 | { 384 | "cell_type": "markdown", 385 | "metadata": {}, 386 | "source": [ 387 | "Now you meet someone else, \"Liz Kaplan\" with the email of \"liz@ka-plan.com\" and want to write a memo with the same instructions, but the only thing that varies are the name and email. This should be easy enough given the way we set up our memo above. First we need to change the variables, `name` and `email`, by setting them to our new data." 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": 11, 393 | "metadata": { 394 | "collapsed": true 395 | }, 396 | "outputs": [], 397 | "source": [ 398 | "name = 'Liz Kaplan'\n", 399 | "email = 'liz@ka-plan.com'" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "So as you can see, we reassign our variables by just setting `variable = 'new data'`. Presto, our variable is then updated." 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": null, 412 | "metadata": { 413 | "collapsed": true 414 | }, 415 | "outputs": [], 416 | "source": [ 417 | "name # 'Liz Kaplan'" 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": null, 423 | "metadata": { 424 | "collapsed": true 425 | }, 426 | "outputs": [], 427 | "source": [ 428 | "email # 'liz@ka-plan.com'" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "metadata": {}, 434 | "source": [ 435 | "Now, if we copy and re-run our previous code, we will see it is automatically updated." 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": null, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "\"Send an email to \" + name + \" at \" + email + \" to say how nice it was meeting yesterday.\"" 445 | ] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "metadata": {}, 450 | "source": [ 451 | "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 change the values of `name` or `email` and operate on their underlying values in the same ways." 452 | ] 453 | }, 454 | { 455 | "cell_type": "markdown", 456 | "metadata": {}, 457 | "source": [ 458 | "### Operating on variables" 459 | ] 460 | }, 461 | { 462 | "cell_type": "markdown", 463 | "metadata": {}, 464 | "source": [ 465 | "Just to hammer this point home let's see what we can now do with the name variable." 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": null, 471 | "metadata": { 472 | "collapsed": true 473 | }, 474 | "outputs": [], 475 | "source": [ 476 | "name" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "metadata": { 483 | "collapsed": true 484 | }, 485 | "outputs": [], 486 | "source": [ 487 | "name.upper()" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": null, 493 | "metadata": { 494 | "collapsed": true 495 | }, 496 | "outputs": [], 497 | "source": [ 498 | "name.title()" 499 | ] 500 | }, 501 | { 502 | "cell_type": "markdown", 503 | "metadata": {}, 504 | "source": [ 505 | "Just like how we are able to directly call methods on a string, we can also call methods on a variable that points to a string. And, if we try to call a method on something that we think is a string, but really is a number, we will see an error." 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": 13, 511 | "metadata": { 512 | "collapsed": true 513 | }, 514 | "outputs": [], 515 | "source": [ 516 | "name = 42" 517 | ] 518 | }, 519 | { 520 | "cell_type": "code", 521 | "execution_count": null, 522 | "metadata": { 523 | "collapsed": true 524 | }, 525 | "outputs": [], 526 | "source": [ 527 | "name.upper()" 528 | ] 529 | }, 530 | { 531 | "cell_type": "markdown", 532 | "metadata": {}, 533 | "source": [ 534 | "We receive the same error from calling `upper` directly on the number `42` as we do when we call `upper` on a variable that points to the number `42`. So, now that we are working with variables, we may run into errors where we thought a variable is one thing, but it is actually something else. Don't worry, this is no big deal. We can just check to see what the variable is." 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": null, 540 | "metadata": { 541 | "collapsed": true 542 | }, 543 | "outputs": [], 544 | "source": [ 545 | "name" 546 | ] 547 | }, 548 | { 549 | "cell_type": "markdown", 550 | "metadata": {}, 551 | "source": [ 552 | "Once we have see what the variable is, we can make our change." 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": null, 558 | "metadata": { 559 | "collapsed": true 560 | }, 561 | "outputs": [], 562 | "source": [ 563 | "name = 'Liz Kaplan'\n", 564 | "name" 565 | ] 566 | }, 567 | { 568 | "cell_type": "markdown", 569 | "metadata": {}, 570 | "source": [ 571 | "### Summary" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "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 reference a variable, we simply type the variable's name. \n", 579 | "\n", 580 | "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 any necessary changes. " 581 | ] 582 | } 583 | ], 584 | "metadata": { 585 | "kernelspec": { 586 | "display_name": "Python 3", 587 | "language": "python", 588 | "name": "python3" 589 | }, 590 | "language_info": { 591 | "codemirror_mode": { 592 | "name": "ipython", 593 | "version": 3 594 | }, 595 | "file_extension": ".py", 596 | "mimetype": "text/x-python", 597 | "name": "python", 598 | "nbconvert_exporter": "python", 599 | "pygments_lexer": "ipython3", 600 | "version": "3.6.4" 601 | } 602 | }, 603 | "nbformat": 4, 604 | "nbformat_minor": 2 605 | } 606 | --------------------------------------------------------------------------------