Skip to content

Commit fea443d

Browse files
committed
added README and re-ran the index.ipynb file to re-assign variables and show the correct string interpolation in example
1 parent be2b8df commit fea443d

File tree

2 files changed

+347
-49
lines changed

2 files changed

+347
-49
lines changed

README.md

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
2+
# Naming things with Variables
3+
4+
> "There are only two hard things in Computer Science: cache invalidation and naming things."
5+
6+
> -- Phil Karlton
7+
8+
> "...But ordinary language is all right."
9+
10+
> Ludwig Wittgenstein
11+
12+
### Objectives
13+
14+
* Learn about how to use variables to give meaning to data
15+
* Learn how to assign a variable to data
16+
* Learn how to declare a variable
17+
* Learn how to reassign a variable
18+
19+
### Declaring and Assigning Variables
20+
21+
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.
22+
23+
24+
```python
25+
"art vandelay"
26+
```
27+
28+
29+
30+
31+
'art vandelay'
32+
33+
34+
35+
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.
36+
37+
So let's use a variables to indicate what each of these strings mean.
38+
39+
40+
```python
41+
42+
```
43+
44+
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:
45+
46+
`variable = data`
47+
48+
Now that we have assigned a variable `email` to a string, we just type the word `email` to see the string again.
49+
50+
51+
```python
52+
email
53+
```
54+
55+
56+
57+
58+
59+
60+
61+
62+
Ok, let's do this with the website too.
63+
64+
65+
```python
66+
website = "vandelay.com"
67+
website
68+
```
69+
70+
71+
72+
73+
'vandelay.com'
74+
75+
76+
77+
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.
78+
79+
80+
```python
81+
name
82+
```
83+
84+
85+
----------------------------------------------------------
86+
87+
NameError Traceback (most recent call last)
88+
89+
<ipython-input-6-9bc0cb2ed6de> in <module>()
90+
----> 1 name
91+
92+
93+
NameError: name 'name' is not defined
94+
95+
96+
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.
97+
98+
99+
```python
100+
name = 'Art Vandalay'
101+
name
102+
```
103+
104+
105+
106+
107+
'Art Vandalay'
108+
109+
110+
111+
So this is assigning and reading a variable. And when we want to see some information again, we can easily find out.
112+
113+
114+
```python
115+
email
116+
```
117+
118+
119+
120+
121+
122+
123+
124+
125+
### Reassigning variables
126+
127+
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:
128+
129+
130+
```python
131+
"Send an email to Art Vandalay at '[email protected]' to tell say how nice it was meeting yesterday."
132+
```
133+
134+
135+
136+
137+
"Send an email to Art Vandalay at '[email protected]' to tell say how nice it was meeting yesterday."
138+
139+
140+
141+
If we construct this message with variables, we have the following:
142+
143+
144+
```python
145+
"Send an email to " + name + " at " + email + " to say how nice it was meeting yesterday."
146+
```
147+
148+
149+
150+
151+
'Send an email to Art Vandalay at [email protected] to say how nice it was meeting yesterday.'
152+
153+
154+
155+
Now, you meet someone else, "Liz Kaplan" with the email of "[email protected]" 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.
156+
157+
158+
```python
159+
name = 'Liz Kaplan'
160+
161+
```
162+
163+
So as you can see, we reassign our variable by just setting `variable = 'new data'`. And our variable is then updated.
164+
165+
166+
```python
167+
name # 'Liz Kaplan'
168+
```
169+
170+
171+
172+
173+
'Liz Kaplan'
174+
175+
176+
177+
178+
```python
179+
180+
```
181+
182+
183+
184+
185+
186+
187+
188+
189+
And now, our previous message is automatically updated.
190+
191+
192+
```python
193+
"Send an email to " + name + " at " + email + " to tell him how nice it was meeting him yesterday."
194+
```
195+
196+
197+
198+
199+
'Send an email to Liz Kaplan at [email protected] to tell him how nice it was meeting him yesterday.'
200+
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 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.
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+
216+
217+
'Liz Kaplan'
218+
219+
220+
221+
222+
```python
223+
name.upper()
224+
```
225+
226+
227+
228+
229+
'LIZ KAPLAN'
230+
231+
232+
233+
234+
```python
235+
name.title()
236+
```
237+
238+
239+
240+
241+
'Liz Kaplan'
242+
243+
244+
245+
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.
246+
247+
248+
```python
249+
name = 42
250+
```
251+
252+
253+
```python
254+
name.upper()
255+
```
256+
257+
258+
----------------------------------------------------------
259+
260+
AttributeError Traceback (most recent call last)
261+
262+
<ipython-input-19-ef33b7f2c4e1> in <module>()
263+
----> 1 name.upper()
264+
265+
266+
AttributeError: 'int' object has no attribute 'upper'
267+
268+
269+
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.
270+
271+
272+
```python
273+
name
274+
```
275+
276+
277+
278+
279+
42
280+
281+
282+
283+
And make the change.
284+
285+
286+
```python
287+
name = 'Liz Kaplan'
288+
name
289+
```
290+
291+
292+
293+
294+
'Liz Kaplan'
295+
296+
297+
298+
### Summary
299+
300+
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.
301+
302+
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.

0 commit comments

Comments
 (0)