Think of a number. Any number. Now, before you forget that number, let’s store it for later. When you think of a number, you are holding that value in your head. If you want to remember it later and your memory is like mine, you write it down on a piece of paper. And if it’s really important, you will put it in a safe place. In computer science, that safe place is a variable. They’re called variables because, well, they’re “capable of being varied or changed”. You can name a variable (almost) anything you want and you can change the value willy-nilly.
I thought of the number 5, so at the Python prompt, I will create a variable with the clever name “number”, and enter my value:
>>> number = 5
Now stop thinking of that number. We’ll come back to it later. If you’re just joining us, you may want to start at the beginning with our introduction to the Python programming language.
Python Variable Assignment Statements
In the above illustration, Attlee, Truman, and Stalin are each thinking of a number. We can treat their names as variables and their respective numbers as values, like so:
>>> Attlee = 7
>>> Truman = 101
>>> Stalin = “a number”
This is called an assignment statement. We use the equal sign (=) to assign a value to a variable. It’s like saying, “Attlee is 7”. If I want to know what number Attlee is thinking about, I simply enter his name at the prompt:
>>> Attlee
7
Now it’s your turn. Try it with the other heads of state.
>>> Truman
101
>>> Stalin
‘a number’
Stalin is being a wisenheimer. ‘a number’ is a string, not a number, and we haven’t covered strings yet. You can change the value of a variable by assigning it a new value. Let’s reassign Stalin’s value to a float. How about pi?
>>> Stalin = 3.14
>>> Stalin
3.14
My head is too full of values to remember anything more than 3.14. Fortunately, we can easily assign the value of pi to a variable using the math module.
>>> import math
>>> math.pi
3.141592653589793
>>> Stalin = math.pi
>>> Stalin
3.141592653589793
As easy as pie.
Variable Naming Conventions and Rules
You may be familiar with algebraic equations, such as the quadratic:
ax² + bx + c = 0
In mathematics, variables are generally single letters like x, y, and z, or Greek symbols like π or θ. Mathematicians often use variables when they don’t know a specific value but are working towards finding it. It’s different in Python. You must assign a value to a variable before you can use it, even if that value is zero or empty. If, for example, I call the variable Guido before assigning it a value:
>>> Guido
I will get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Guido' is not defined</module></stdin>
Variable assignment works left to right.
>>>> Guido = 0
Is acceptable. As is:
>>> Guido = ""
And even:
>>>> Guido = False
But the following will give you a nasty error.
>>> 0 = Guido
>>> ‘’ = Guido
>>> False = Guido
The first two gave you an error that read:
File "<stdin>", line 1
SyntaxError: can't assign to literal</stdin>
But the last one gave you an error that read:
File "<stdin>", line 1
SyntaxError: assignment to keyword</stdin>
That’s because False is a reserved word in Python. It’s what computer scientists refer to as a Boolean value, and you can’t use it as a variable identifier. There’s a Python module called keyword. It has a function called kwlist. Importing keyword and calling kwlist will return a list of Python’s keywords. Try it:
>>>> import keyword
>>> keyword.kwlist
Those are all the words you can’t use. But that’s okay. When it comes to variable names, the sky is the limit! Well, almost. There are some rules you need to follow and some conventions you ought.
The Rules
-
Variables names must start with a letter or an underscore, such as:
- _underscore
- underscore_
-
The remainder of your variable name may consist of letters, numbers and underscores.
- password1
- n00b
- underscores
-
Names are case sensitive.
- casesensitive, CASESENSITIVE, and Case_Sensitive are each a different variable.
The Conventions
-
Readability is very important. Which of the following is easiest to read? I’m hoping you’ll say the first example.
- python_puppet
- pythonpuppet
- pythonPuppet
-
Descriptive names are very useful. If you are writing a program that adds up all of the bad puns made in this book, which do you think is the better variable name?
- totalbadpuns
- super_bad
- Avoid using the lowercase letter ‘l’, uppercase ‘O’, and uppercase ‘I’. Why? Because the l and the I look a lot like each other and the number 1. And O looks a lot like 0.
In our next lesson we will put all of this to good use making variable sandwiches. Mmm, just like Mom used to make.