You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+75-19
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@
20
20
21
21
### Declaring and Assigning Variables
22
22
23
-
So far we have only 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**.
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
24
25
25
26
26
```python
@@ -34,20 +34,20 @@ So far we have only worked with data -- strings, numbers, and booleans. In this
34
34
35
35
36
36
37
-
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**: `"[email protected]"`, `"Ceo"`, `"7285553334"`, `"vandelay.com"`. There's a lot to keep track of.
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**: `"[email protected]"`, `"Ceo"`, `"7285553334"`, `"vandelay.com"`. There's a lot to keep track of.
38
38
39
-
So let's use a variables to indicate what each of these strings mean.
39
+
So, let's use variables to indicate what each of these strings mean.
> 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.
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
47
48
-
In programming terms, we say that just "declared a variable `email` and assigned it to the string `"[email protected]"`". And to do so, we'll follow the procedure above:
48
+
In programming terms, we say that we just declared a variable,`email`, and assigned it to the string,`"[email protected]"`. To do so, we'll follow the procedure below:
49
49
50
-
`variable = data`
50
+
variable = data
51
51
52
52
Now that we have assigned a variable `email` to a string, we just type the word `email` to see the string again.
53
53
@@ -56,9 +56,9 @@ Now that we have assigned a variable `email` to a string, we just type the word
56
56
email
57
57
```
58
58
59
-
> Press shift + enter on the gray box above to see what `email` equals.
59
+
> *remember to press shift + enter on the gray box above to see the value of our variable, *`email`*.*
60
60
61
-
Ok, let's do this with the website too.
61
+
Now let's try this with the website:
62
62
63
63
64
64
```python
@@ -98,34 +98,90 @@ So this is assigning and reading a variable. And when we want to see some infor
98
98
99
99
```python
100
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
+
101
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
102
143
```
103
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
+
104
154
### Reassigning variables
105
155
106
-
Now that we have this data, you can imagine using it for some instructions. For example, say you want to write some yourself a memo on who and how to reach out to someone you just met. Here's the message:
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:
107
157
108
158
109
159
```python
110
-
"Send an email to Art Vandalay at '[email protected]' to tell say how nice it was meeting yesterday."
160
+
"Send an email to Art Vandalay at '[email protected]' to say how nice it was meeting yesterday."
111
161
```
112
162
113
163
If we construct this message with variables, we can write the following:
"Send an email to "+ name +" at "+ email +" to say how nice it was meeting yesterday."
118
174
```
119
175
120
-
Now you meet someone else, "Liz Kaplan" with the email of "[email protected]" and want to write a memo with the same instructions, but the only thing that varies are the name and email. So then this is easy enough. First we change set the variables `name` and `email` equal to different data.
176
+
Now you meet someone else, "Liz Kaplan" with the email of "[email protected]" 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.
And now, if we copy and run our previous code again, it is automatically updated.
196
+
Now, if we copy and re-run our previous code, we will see it is automatically updated.
141
197
142
198
143
199
```python
144
-
"Send an email to "+ name +" at "+ email +" to tell him how nice it was meeting him yesterday."
200
+
"Send an email to "+ name +" at "+ email +" to say how nice it was meeting yesterday."
145
201
```
146
202
147
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.
@@ -165,7 +221,7 @@ name.upper()
165
221
name.title()
166
222
```
167
223
168
-
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 method on something that you think is a string, but really is a number, you will see an error.
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.
169
225
170
226
171
227
```python
@@ -177,14 +233,14 @@ name = 42
177
233
name.upper()
178
234
```
179
235
180
-
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.
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.
181
237
182
238
183
239
```python
184
240
name
185
241
```
186
242
187
-
And make the change.
243
+
Once we have see what the variable is, we can make our change.
188
244
189
245
190
246
```python
@@ -194,6 +250,6 @@ name
194
250
195
251
### Summary
196
252
197
-
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.
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.
198
254
199
-
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.
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.
0 commit comments