Skip to content

Commit d26c2ea

Browse files
committed
good and *BAD* variables, other cleanups
1 parent 3b44341 commit d26c2ea

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

basics/variables.md

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,22 @@ NameError: name 'thingy' is not defined
5454
>>>
5555
```
5656

57-
Variables are simple to understand, but there's a few details that we
57+
Variables are simple to understand, but there are a few details that we
5858
need to keep in mind:
5959

6060
- Variables always point to a value, **they never point to other
61-
variables**. That's why the arrows in our diagrams always go left
62-
to right.
61+
variables**. That's why the arrows in our diagrams always go left
62+
to right.
6363
- Multiple variables can point to the same value, but one variable
64-
cannot point to multiple values.
64+
cannot point to multiple values.
6565
- The values that variables point to can point to other values also.
66-
We'll learn more about that when we'll talk about
67-
[lists](lists-and-tuples.md).
66+
We'll learn more about that when we'll talk about
67+
[lists](lists-and-tuples.md).
6868

6969
Variables are an important part of most programming languages, and they
7070
allow programmers to write much larger programs than they could write
7171
without variables.
7272

73-
Variable names can be multiple characters long. They can contain
74-
uppercase characters, numbers and some other characters, but most of the
75-
time we should use simple, lowercase variable names. You can also use
76-
underscores.
77-
78-
```python
79-
>>> magic_number = 123
80-
>>> greeting = "Hello World!"
81-
>>>
82-
```
83-
8473
Variable names are case-sensitive, like many other things in Python.
8574

8675
```python
@@ -96,7 +85,7 @@ Variable names are case-sensitive, like many other things in Python.
9685
>>>
9786
```
9887

99-
Python also has some words that cannot be used as variable names
88+
There are also words that cannot be used as variable names
10089
because they have a special meaning. They are called **keywords**, and
10190
we can run `help('keywords')` to see the full list if we want to.
10291
We'll learn to use most of them later in this tutorial. Trying to use a
@@ -158,6 +147,36 @@ variable called hello and then type hello:
158147
>>>
159148
```
160149

150+
## Good and bad variable names
151+
152+
Variable names can be multiple characters long. They can contain
153+
uppercase characters, numbers and some other characters, but most of the
154+
time we should use simple, lowercase variable names. We can also use
155+
underscores. For example, these variable names are good:
156+
157+
```python
158+
>>> magic_number = 123
159+
>>> greeting = "Hello World!"
160+
>>>
161+
```
162+
163+
Don't use variable names like this, **these variables are _bad_**:
164+
165+
```python
166+
>>> magicNumber = 3.14
167+
>>> Greeting = "Hello there!"
168+
>>> x = "Hello again!"
169+
>>>
170+
```
171+
172+
All of these variables work just fine, but other Python programmers
173+
don't want you to use them. Most Python code doesn't use variable names
174+
that contain UpperCase letters like `magicNumber` and `Greeting`, so
175+
other people reading your code will think it looks weird if you use
176+
them. The problem with `x` is that it's too short, and people have no
177+
idea what it is. Remember that mathematicians like figuring out what x
178+
is, but programmers hate that.
179+
161180
## Booleans
162181

163182
There are two Boolean values, True and False. In Python, and in many
@@ -224,7 +243,8 @@ None
224243

225244
Another confusing thing is that if we do something weird to None we get
226245
error messages that talk about NoneType object. The NoneType object they
227-
are talking about is always None.
246+
are talking about is always None. We'll learn more about what attributes
247+
and calling are later.
228248

229249
```python
230250
>>> None.hello # None has no attribute 'hello'
@@ -240,16 +260,15 @@ TypeError: 'NoneType' object is not callable
240260

241261
## Other comparing operators
242262

243-
So far we've used `==`, but there are other operators also. At this
244-
point, this list probably looks awfully long, but it's actually pretty
245-
easy to learn.
263+
So far we've used `==`, but there are other operators also. This list
264+
probably looks awfully long, but it's actually quite easy to learn.
246265

247266
| Usage | Description | True examples |
248267
|-----------|-----------------------------------|-----------------------|
249268
| `a == b` | a is equal to b | `1 == 1` |
250269
| `a != b` | a is not equal to b | `1 != 2` |
251-
| `a > b` | a is greater than b | `2 > 1` |
252-
| `a >= b` | a is greater than or equal to b | `2 >= 1`, `1 >= 1` |
270+
| `a > b` | a is bigger than b | `2 > 1` |
271+
| `a >= b` | a is bigger than or equal to b | `2 >= 1`, `1 >= 1` |
253272
| `a < b` | a is less than b | `1 < 2` |
254273
| `a <= b` | a is less than or equal to b | `1 <= 2`, `1 <= 1` |
255274

@@ -261,15 +280,24 @@ b are Booleans.
261280
| `a and b` | a is True and b is True | `1 == 1 and 2 == 2` |
262281
| `a or b` | a is True, b is True or they're both True | `False or 1 == 1`, `True or True` |
263282

264-
Another way to combine operations is chaining. For example, `a < b < c`
265-
does the same thing as `a < b and b < c`.
266-
267283
`not` can be used for negations. If `value` is True, `not value` is
268284
False, and if `value` is False, `not value` is True.
269285

270286
There's also `is`, but don't use it instead of `==` unless you know
271287
what you are doing. We'll learn more about it later.
272288

289+
## Summary
290+
291+
- Variables have a name and a value. We can create or change variables
292+
with `name = value`.
293+
- `thing += stuff` does the same thing as `thing = thing + stuff`.
294+
- Use lowercase variable names and remember that programmers hate
295+
figuring out what x is.
296+
- `=` means assigning and `==` means comparing.
297+
- True and False are Booleans. Comparing values results in a Boolean.
298+
- None is a value that we'll find useful later. When error messages say
299+
`NoneType object` they mean None.
300+
273301
***
274302

275303
If you have trouble with this tutorial please [tell me about

0 commit comments

Comments
 (0)