Skip to content

Commit 45fae1f

Browse files
committed
explain variables more
1 parent d00265d commit 45fae1f

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

images/drawings.odg

-1.14 KB
Binary file not shown.

images/variables1.png

3.12 KB
Loading

images/variables2.png

3.65 KB
Loading

variables.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,43 @@ Variables are easy to understand. They simply **point to data**.
66

77
```py
88
>>> a = 1 # create a variable called a that points to 1
9-
>>> a # get the value that a points to
9+
>>> b = 2 # create another variable
10+
>>> a # get the value that the variable points to
1011
1
12+
>>> b
13+
2
1114
>>>
1215
```
1316

17+
Let's draw a diagram of these variables.
18+
19+
![Variable diagram](images/variables1.png)
20+
1421
We can also change the value of a variable after setting it.
1522

1623
```py
17-
>>> a = 2 # make it point to 2 instead
24+
>>> a = 2 # make a point to 2 instead of 1
1825
>>> a
1926
2
2027
>>>
2128
```
2229

30+
So now our diagram looks like this:
31+
32+
![Variable diagram](images/variables2.png)
33+
34+
Setting a variable to another variable gets the value of the other
35+
variable and sets the first variable to point to that value.
36+
37+
```py
38+
>>> a = 1
39+
>>> b = a # this makes b point to 1, not a
40+
>>> a = 5
41+
>>> b # b didn't change when a changed
42+
1
43+
>>>
44+
```
45+
2346
Trying to access a variable that is not defined is an error.
2447

2548
```py
@@ -30,14 +53,26 @@ NameError: name 'thingy' is not defined
3053
>>>
3154
```
3255

56+
Variables are simple to understand, but there's a few details that we
57+
need to keep in mind:
58+
59+
- Variables always point to a value, **they never point to other
60+
variables**. That's why the arrows in our diagrams always go left
61+
to right.
62+
- Multiple variables can point to the same value, but one variable
63+
cannot point to multiple values.
64+
65+
- The values that variables point to can point to other values also.
66+
We'll learn more about that when we'll talk about [lists](lists.md).
67+
3368
Variables are an important part of most programming languages, and they
3469
allow programmers to write much larger programs than they could write
3570
without variables.
3671

3772
Variable names can be multiple characters long. They can contain
38-
uppercase characters, numbers and some other characters, but most of
39-
the time you should use simple, lowercase variable names. You can also
40-
use underscores.
73+
uppercase characters, numbers and some other characters, but most of the
74+
time you should use simple, lowercase variable names. You can also use
75+
underscores.
4176

4277
```py
4378
>>> number_one = 1

0 commit comments

Comments
 (0)