## Calm Down or You’re Going to Lose Your Variables

On how variables and values work in Python

The word “variable” sounds intimidatingly technical but it’s really just something that, well, varies — or, at least, it can vary. Let’s say you’re starting an exercise routine. You’ve gotten flabby and out of shape and your weight is up in the 230 pounds range (damn this scale!). So, you assign a number to your weight as follows:

``weight = 230``

Type that into your Shell and hit return. Now, if you type the word weight into your shell and hit return, you should get the number 230.

Let’s say you go on an exercise program and lose a little weight, then a little more, then maybe gain some weight back (the story of my life), and then lose some more again. You can keep assigning different numbers to the variable called weight. Therefore, you’re using the same word but assigning a variety of numbers to it. Variable…vary. Very logical.

See the below for an example of how this works as you assign different numbers to the variable we call weight:

Now, let’s say you’re talking about the cost of a tablet computer. When you buy it, it’s worth \$800. A couple of year later, it’s worth about \$300. And a year after that, you can only get \$100 for it on eBay. So, our variable called tablet could be assigned various values depending on its age.

And, indeed, “value” is what we call the numbers that we assign to variables. Therefore, the last value of weight was 205 in the first example above. The last value of tablet we discussed was \$100. The value of variables (just like human values) can shift over time.

I’ve been using a bit of extra jargon in this particular post: that is, the word “assign.” When you assign the value \$100 to the variable tablet, you write it like this:

`tablet = \$100`

You’re using the equals sign on your keyboard to assign a value. That would all be fine and dandy if there weren’t any math in computer programming. But there is. Sometimes you want your equals sign to actually mean “equals” rather than “assign.” When you add a couple of numbers in the real world, for example, you typically use an equals sign, as in 2 + 2 = 4.

`>>>2 + 2 = 4`

Now, hit Enter. If you’re doing it right (or, in this case, wrong), you’ll get another error message:

```SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
```

Thanks for the hint, Python! Indeed I did! So, let’s try putting this into the Shell:

`2 + 2 == 4`

Now hit your Enter key again. This time, Python should spit out the word True. You see, Python refused to use the single equals sign for math. It always wants the double equals sign if you’re doing math.

This can get annoying, but you’ve just got to live with it a while and allow it to become second nature. Think of it as one of those vexing habits of someone you love. You live with it because you love them. That habit still annoys the crap out of you until the day it becomes expected and even endearing. Welcome to Python world (or, for that matter, the world of programming).

At times, you can lose track of your variables. You think you’ve assigned some value to a variable and then you plug it into your code. Python, however, disagrees. It thinks you haven’t assigned a value to that variable yet. So, you get into a spat. Python always wins those spats, of course, because it is more logical and stubborn than you are.

You can get feel for Python literalness by looking at this:

You can see I’ve assigned the number six to Joe. If I type in Joe, Python spits out the number six. But then I type in Jo, and Python throws up an error message called a NameError. This time, it helpfully asks “did you mean: ‘Joe'”?

Which is groovy because when I first learned Python, I did not get those kind of helpful hints. Python has gotten a little smarter and more civil in recent years as it has mellowed.

The bottom line, however, is that should try to avoid losing track of your variables. If you’re programming, you’re going to need them.

`Featured image is a cartoon created by Gunny Wolf. Author is Marines from Arlington, VA, United States. See Wikimedia Commons at https://commons.wikimedia.org/wiki/File:Calm_Down_(8455073021).jpg`