Skip to content

Commit d25d907

Browse files
committed
update 03 assignment
1 parent 58baf2e commit d25d907

File tree

1 file changed

+41
-94
lines changed

1 file changed

+41
-94
lines changed

Day_1/03_Variables_Assignment.ipynb

Lines changed: 41 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {
6-
"collapsed": true
7-
},
5+
"metadata": {},
86
"source": [
97
"# Variables and Assignment\n",
108
"\n",
@@ -13,25 +11,23 @@
1311
"- Challenges: 10 min\n",
1412
"\n",
1513
"**Questions**\n",
16-
"- \"How can I store data in programs?\"\n",
14+
"- How can I store data in programs?\n",
1715
"\n",
1816
"**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",
2119
"\n",
2220
"* * * * *"
2321
]
2422
},
2523
{
2624
"cell_type": "markdown",
27-
"metadata": {
28-
"collapsed": true
29-
},
25+
"metadata": {},
3026
"source": [
31-
"## Use variables to store values.\n",
27+
"## Use variables to store values\n",
3228
"\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",
3531
"* The variable is created when a value is assigned to it.\n",
3632
"* Here's Python code that assigns an age to a variable `age`\n",
3733
" and a name in quotation marks to a variable `first_name`.\n"
@@ -51,16 +47,16 @@
5147
"cell_type": "markdown",
5248
"metadata": {},
5349
"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",
6056
"\n",
61-
"## Use `print` to display values.\n",
57+
"## Use `print` to display values\n",
6258
"\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",
6460
"* Call the function (i.e., tell Python to run it) by using its name.\n",
6561
"* Provide values to the function (e.g., things to print) in parentheses.\n"
6662
]
@@ -78,17 +74,17 @@
7874
"cell_type": "markdown",
7975
"metadata": {},
8076
"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",
8379
"\n",
84-
"## Variables persist between cells.\n",
80+
"## Variables persist between cells\n",
8581
"\n",
8682
"* Variables defined in one cell exist in all following cells.\n",
8783
"* Notebook cells are just a way to organize a program:\n",
8884
" as far as Python is concerned,\n",
8985
" all of the source code is one long set of instructions.\n",
9086
"\n",
91-
"## Variables must be created before they are used.\n",
87+
"## Variables must be created before they are used\n",
9288
"\n",
9389
"* If a variable doesn't exist yet, or if the name has been mis-spelled,\n",
9490
" Python reports an error.\n"
@@ -110,15 +106,15 @@
110106
"* The last line of an error message is usually the most informative.\n",
111107
"* We will look at error messages in detail [later](https://github.com/dlab-berkeley/python-intensive/blob/master/Day_3/15_Errors.ipynb).\n",
112108
"\n",
113-
"## Python is case-sensitive.\n",
109+
"## Python is case-sensitive\n",
114110
"\n",
115111
"* Python thinks that upper- and lower-case letters are different,\n",
116112
" so `Name` and `name` are different variables.\n",
117113
"* Again,\n",
118114
" there are conventions around using upper-case letters at the start of variable names\n",
119115
" so we will use lower-case letters for now.\n",
120116
"\n",
121-
"## Use meaningful variable names.\n",
117+
"## Use meaningful variable names\n",
122118
"\n",
123119
"* Python doesn't care what you call variables as long as they obey the rules\n",
124120
" (alphanumeric characters and the underscore).\n"
@@ -145,7 +141,7 @@
145141
"## Variables can be used in calculations.\n",
146142
"\n",
147143
"* 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."
149145
]
150146
},
151147
{
@@ -158,56 +154,14 @@
158154
"print('Age in three years:', age)"
159155
]
160156
},
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-
},
202157
{
203158
"cell_type": "markdown",
204159
"metadata": {},
205160
"source": [
206161
"# `%who` and `%whos`\n",
207162
"\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:"
211165
]
212166
},
213167
{
@@ -223,7 +177,7 @@
223177
"cell_type": "markdown",
224178
"metadata": {},
225179
"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:"
227181
]
228182
},
229183
{
@@ -239,10 +193,10 @@
239193
"cell_type": "markdown",
240194
"metadata": {},
241195
"source": [
242-
"## Challenge 1: Making and Printing Variables\n",
196+
"## Challenge 1: Creating and Printing Variables\n",
243197
"\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]`"
246200
]
247201
},
248202
{
@@ -258,7 +212,7 @@
258212
"source": [
259213
"## Challenge 2: Swapping Values\n",
260214
"\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",
262216
"\n",
263217
"In simple terms, what do the last three lines of this program do?"
264218
]
@@ -350,29 +304,22 @@
350304
"source": [
351305
"*****\n",
352306
"\n",
353-
"## Keypoints\n",
307+
"## Key Points\n",
354308
"\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."
362316
]
363-
},
364-
{
365-
"cell_type": "code",
366-
"execution_count": null,
367-
"metadata": {},
368-
"outputs": [],
369-
"source": []
370317
}
371318
],
372319
"metadata": {
373320
"anaconda-cloud": {},
374321
"kernelspec": {
375-
"display_name": "Python 3",
322+
"display_name": "Python 3 (ipykernel)",
376323
"language": "python",
377324
"name": "python3"
378325
},
@@ -386,7 +333,7 @@
386333
"name": "python",
387334
"nbconvert_exporter": "python",
388335
"pygments_lexer": "ipython3",
389-
"version": "3.8.3"
336+
"version": "3.9.7"
390337
},
391338
"toc": {
392339
"base_numbering": 1,
@@ -432,5 +379,5 @@
432379
}
433380
},
434381
"nbformat": 4,
435-
"nbformat_minor": 1
382+
"nbformat_minor": 4
436383
}

0 commit comments

Comments
 (0)