|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "markdown",
|
5 |
| - "metadata": { |
6 |
| - "collapsed": true |
7 |
| - }, |
| 5 | + "metadata": {}, |
8 | 6 | "source": [
|
9 | 7 | "# Variables and Assignment\n",
|
10 | 8 | "\n",
|
|
13 | 11 | "- Challenges: 10 min\n",
|
14 | 12 | "\n",
|
15 | 13 | "**Questions**\n",
|
16 |
| - "- \"How can I store data in programs?\"\n", |
| 14 | + "- How can I store data in programs?\n", |
17 | 15 | "\n",
|
18 | 16 | "**Learning Objectives**\n",
|
19 |
| - "- \"Write programs that [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) scalar values to variables and perform calculations with those values.\"\n", |
20 |
| - "- \"Correctly trace value changes in programs that use scalar assignment.\"\n", |
| 17 | + "- Write programs that [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) scalar values to variables and perform calculations with those values.\n", |
| 18 | + "- Correctly trace value changes in programs that use scalar assignment.\n", |
21 | 19 | "\n",
|
22 | 20 | "* * * * *"
|
23 | 21 | ]
|
24 | 22 | },
|
25 | 23 | {
|
26 | 24 | "cell_type": "markdown",
|
27 |
| - "metadata": { |
28 |
| - "collapsed": true |
29 |
| - }, |
| 25 | + "metadata": {}, |
30 | 26 | "source": [
|
31 |
| - "## Use variables to store values.\n", |
| 27 | + "## Use variables to store values\n", |
32 | 28 | "\n",
|
33 |
| - "* Variables are names for values.\n", |
34 |
| - "* In Python the `=` symbol assigns the value on the right to the name on the left.\n", |
| 29 | + "* Variables are placeholders for useful values. They have meaningful names, which makes it easier to reuse those values.\n", |
| 30 | + "* In Python, the `=` symbol assigns the value on the right to the name on the left.\n", |
35 | 31 | "* The variable is created when a value is assigned to it.\n",
|
36 | 32 | "* Here's Python code that assigns an age to a variable `age`\n",
|
37 | 33 | " and a name in quotation marks to a variable `first_name`.\n"
|
|
51 | 47 | "cell_type": "markdown",
|
52 | 48 | "metadata": {},
|
53 | 49 | "source": [
|
54 |
| - "* Variable names:\n", |
55 |
| - " * cannot start with a digit\n", |
56 |
| - " * cannot contain spaces, quotation marks, or other punctuation\n", |
57 |
| - " * *may* contain an underscore (typically used to separate words in long variable names)\n", |
58 |
| - "* Underscores at the start like `__alistairs_real_age` have a special meaning\n", |
59 |
| - " so we won't do that until we understand the convention.\n", |
| 50 | + "* Variable names must follow a few rules:\n", |
| 51 | + " * They cannot start with a digit.\n", |
| 52 | + " * They cannot contain spaces, quotation marks, or other punctuation.\n", |
| 53 | + " * They *may* contain an underscore (typically used to separate words in long variable names).\n", |
| 54 | + "* Underscores at the beginning of a variable name, such as `__first_name`, have a special meaning.\n", |
| 55 | + " We won't use this format until we understand the convention.\n", |
60 | 56 | "\n",
|
61 |
| - "## Use `print` to display values.\n", |
| 57 | + "## Use `print` to display values\n", |
62 | 58 | "\n",
|
63 |
| - "* Python has a built-in function called `print` that prints things as text.\n", |
| 59 | + "* Python has a built-in function called `print` that prints output given some inputs.\n", |
64 | 60 | "* Call the function (i.e., tell Python to run it) by using its name.\n",
|
65 | 61 | "* Provide values to the function (e.g., things to print) in parentheses.\n"
|
66 | 62 | ]
|
|
78 | 74 | "cell_type": "markdown",
|
79 | 75 | "metadata": {},
|
80 | 76 | "source": [
|
81 |
| - "* `print` automatically puts a single space between items to separate them.\n", |
82 |
| - "* And wraps around to a new line at the end.\n", |
| 77 | + "* `print` automatically puts a single space between different inputs to separate them.\n", |
| 78 | + "* It also adds a new line at the end. Try entering multiple `print` statements to see this.\n", |
83 | 79 | "\n",
|
84 |
| - "## Variables persist between cells.\n", |
| 80 | + "## Variables persist between cells\n", |
85 | 81 | "\n",
|
86 | 82 | "* Variables defined in one cell exist in all following cells.\n",
|
87 | 83 | "* Notebook cells are just a way to organize a program:\n",
|
88 | 84 | " as far as Python is concerned,\n",
|
89 | 85 | " all of the source code is one long set of instructions.\n",
|
90 | 86 | "\n",
|
91 |
| - "## Variables must be created before they are used.\n", |
| 87 | + "## Variables must be created before they are used\n", |
92 | 88 | "\n",
|
93 | 89 | "* If a variable doesn't exist yet, or if the name has been mis-spelled,\n",
|
94 | 90 | " Python reports an error.\n"
|
|
110 | 106 | "* The last line of an error message is usually the most informative.\n",
|
111 | 107 | "* We will look at error messages in detail [later](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_3/15_Errors.ipynb).\n",
|
112 | 108 | "\n",
|
113 |
| - "## Python is case-sensitive.\n", |
| 109 | + "## Python is case-sensitive\n", |
114 | 110 | "\n",
|
115 | 111 | "* Python thinks that upper- and lower-case letters are different,\n",
|
116 | 112 | " so `Name` and `name` are different variables.\n",
|
117 | 113 | "* Again,\n",
|
118 | 114 | " there are conventions around using upper-case letters at the start of variable names\n",
|
119 | 115 | " so we will use lower-case letters for now.\n",
|
120 | 116 | "\n",
|
121 |
| - "## Use meaningful variable names.\n", |
| 117 | + "## Use meaningful variable names\n", |
122 | 118 | "\n",
|
123 | 119 | "* Python doesn't care what you call variables as long as they obey the rules\n",
|
124 | 120 | " (alphanumeric characters and the underscore).\n"
|
|
145 | 141 | "## Variables can be used in calculations.\n",
|
146 | 142 | "\n",
|
147 | 143 | "* We can use variables in calculations just as if they were values.\n",
|
148 |
| - " * Remember, we assigned 42 to `age` a few lines ago." |
| 144 | + "* Remember, we assigned 42 to `age` a few lines ago." |
149 | 145 | ]
|
150 | 146 | },
|
151 | 147 | {
|
|
158 | 154 | "print('Age in three years:', age)"
|
159 | 155 | ]
|
160 | 156 | },
|
161 |
| - { |
162 |
| - "cell_type": "markdown", |
163 |
| - "metadata": {}, |
164 |
| - "source": [ |
165 |
| - "## Namespace\n", |
166 |
| - "\n", |
167 |
| - "To display the scope namespace of variables we can use the `dir` function:\n" |
168 |
| - ] |
169 |
| - }, |
170 |
| - { |
171 |
| - "cell_type": "code", |
172 |
| - "execution_count": null, |
173 |
| - "metadata": {}, |
174 |
| - "outputs": [], |
175 |
| - "source": [ |
176 |
| - "dir()" |
177 |
| - ] |
178 |
| - }, |
179 |
| - { |
180 |
| - "cell_type": "markdown", |
181 |
| - "metadata": {}, |
182 |
| - "source": [ |
183 |
| - "To get a dictionary of local variables, we can call `locals`:" |
184 |
| - ] |
185 |
| - }, |
186 |
| - { |
187 |
| - "cell_type": "code", |
188 |
| - "execution_count": null, |
189 |
| - "metadata": {}, |
190 |
| - "outputs": [], |
191 |
| - "source": [ |
192 |
| - "locals()" |
193 |
| - ] |
194 |
| - }, |
195 |
| - { |
196 |
| - "cell_type": "markdown", |
197 |
| - "metadata": {}, |
198 |
| - "source": [ |
199 |
| - "With both functions, we see a lot of predefined variables in the Python and Jupyter environment, but also `age`, `first_name`, `flabadab`, and `ewr_422_yY`, which we created above." |
200 |
| - ] |
201 |
| - }, |
202 | 157 | {
|
203 | 158 | "cell_type": "markdown",
|
204 | 159 | "metadata": {},
|
205 | 160 | "source": [
|
206 | 161 | "# `%who` and `%whos`\n",
|
207 | 162 | "\n",
|
208 |
| - "If `dir()` and `locals()` are too confusing, try these [magic](https://towardsdatascience.com/top-10-magic-commands-in-python-to-boost-your-productivity-1acac061c7a9) [commands](https://ipython.readthedocs.io/en/stable/interactive/magics.html) instead. \n", |
209 |
| - "\n", |
210 |
| - "`%who` will return the variables you have saved:" |
| 163 | + "* There are a couple \"magic\" commands you can run to help you keep track of your variables.\n", |
| 164 | + "* `%who` will return the variables you have saved:" |
211 | 165 | ]
|
212 | 166 | },
|
213 | 167 | {
|
|
223 | 177 | "cell_type": "markdown",
|
224 | 178 | "metadata": {},
|
225 | 179 | "source": [
|
226 |
| - "`%whos` will display the variables, their type, and information about the data they contain:" |
| 180 | + "* `%whos` will display the variables, their type, and information about the data they contain:" |
227 | 181 | ]
|
228 | 182 | },
|
229 | 183 | {
|
|
239 | 193 | "cell_type": "markdown",
|
240 | 194 | "metadata": {},
|
241 | 195 | "source": [
|
242 |
| - "## Challenge 1: Making and Printing Variables\n", |
| 196 | + "## Challenge 1: Creating and Printing Variables\n", |
243 | 197 | "\n",
|
244 |
| - "1. Make 3 variables: `name` (with your full name), `city` (where you were born) and `year` (when you were born).\n", |
245 |
| - "2. Print these three variables so that it prints `[your name] was born in [city] in [year]`" |
| 198 | + "1. Create 3 variables: `name` (with your full name), `city` (where you were born) and `year` (when you were born).\n", |
| 199 | + "2. Print these three variables with the `print` function so that it prints `[your name] was born in [city] in [year]`" |
246 | 200 | ]
|
247 | 201 | },
|
248 | 202 | {
|
|
258 | 212 | "source": [
|
259 | 213 | "## Challenge 2: Swapping Values\n",
|
260 | 214 | "\n",
|
261 |
| - "Draw a table showing the values of the variables in this program after each statement is executed.\n", |
| 215 | + "Create a table showing the values of the variables in this program after each statement is executed.\n", |
262 | 216 | "\n",
|
263 | 217 | "In simple terms, what do the last three lines of this program do?"
|
264 | 218 | ]
|
|
350 | 304 | "source": [
|
351 | 305 | "*****\n",
|
352 | 306 | "\n",
|
353 |
| - "## Keypoints\n", |
| 307 | + "## Key Points\n", |
354 | 308 | "\n",
|
355 |
| - "1. \"Use variables to store values.\"\n", |
356 |
| - "2. \"Use `print` to display values.\"\n", |
357 |
| - "3. \"Variables persist between cells.\"\n", |
358 |
| - "4. \"Variables must be created before they are used.\"\n", |
359 |
| - "5. \"Python is case-sensitive.\"\n", |
360 |
| - "6. \"Variables can be used in calculations.\"\n", |
361 |
| - "7. \"Use meaningful variable names.\"" |
| 309 | + "1. Use variables to store meaningful values.\n", |
| 310 | + "2. Use `print` to display values.\n", |
| 311 | + "3. Variables persist between cells.\n", |
| 312 | + "4. Variables must be created before they are used.\n", |
| 313 | + "5. Python is case-sensitive.\n", |
| 314 | + "6. Variables can be used in calculations.\n", |
| 315 | + "7. Use meaningful variable names." |
362 | 316 | ]
|
363 |
| - }, |
364 |
| - { |
365 |
| - "cell_type": "code", |
366 |
| - "execution_count": null, |
367 |
| - "metadata": {}, |
368 |
| - "outputs": [], |
369 |
| - "source": [] |
370 | 317 | }
|
371 | 318 | ],
|
372 | 319 | "metadata": {
|
373 | 320 | "anaconda-cloud": {},
|
374 | 321 | "kernelspec": {
|
375 |
| - "display_name": "Python 3", |
| 322 | + "display_name": "Python 3 (ipykernel)", |
376 | 323 | "language": "python",
|
377 | 324 | "name": "python3"
|
378 | 325 | },
|
|
386 | 333 | "name": "python",
|
387 | 334 | "nbconvert_exporter": "python",
|
388 | 335 | "pygments_lexer": "ipython3",
|
389 |
| - "version": "3.8.3" |
| 336 | + "version": "3.9.7" |
390 | 337 | },
|
391 | 338 | "toc": {
|
392 | 339 | "base_numbering": 1,
|
|
432 | 379 | }
|
433 | 380 | },
|
434 | 381 | "nbformat": 4,
|
435 |
| - "nbformat_minor": 1 |
| 382 | + "nbformat_minor": 4 |
436 | 383 | }
|
0 commit comments