So far, I have only used variables called x, y, and z (and one erroneous days_in_a_year). However, you are free to choose the names of your variables as you like them, provided that you follow a few simple rules, namely:

“Reserved words” (or “keywords”) are:

False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise

You can use capitals and lower case letters in variable names, but you should realize that variable names are case sensitive, i.e., the variable world is not the same as the variable World.

Conventions

Programmers follow many conventions when choosing variable names. The major ones are the following:

You should try to stick to these conventions for your own code. In particular the convention of choosing meaningful variable names is important to follow, because meaningful variable names make code readable and maintainable. Look, for instance, at the following code:

a = 3.14159265
b = 7.5
c = 8.25
d = a * b * b * c / 3
print( d )

Do you understand what this code does? You probably see that a seems to be an approximation of \(\pi\), but what is d supposed to be?

I can tell you that this code calculates the volume of a cone. You probably would not have guessed that, but that is what it does. Now I ask you to change the code to calculate the volume of a cone that is 4 meters high. What change will you make? If height is part of the calculation, it is probably b or c. But which is it? Maybe if you know a bit of maths and you look at the calculation of d, you realize that b is squared in this calculation, which seems to refer to the base of the cone, which is a circle. So it is probably c. But you cannot be sure.

Now look at the following, equivalent code:

pi = 3.14159265
radius = 7.5
height = 8.25
volume_of_cone = pi * radius * radius * height / 3
print( volume_of_cone )

This is much more readable, right? If I asked you to look at this code and tell me what it does, and make the requested change, I don’t expect you to hesitate in answering.

Such code with meaningful variable names tends to become “self-documenting”; you do not need to add any comments to make the user understand what it does and how it does it. Still, in the code above a line of comment that says:

# calculation of volume of a cone with radius 7.5 and height 8.25

would not be misplaced.

Practicing with variable names

Exercise

In the code block below, the value 1 is assigned to a number of (potential) variable names. Some of these are legal, others are not. Identify the illegal variable names, and explain why they are illegal.

classification = 1   # 1
Classification = 1   # 2
cl@ssification = 1   # 3
class1f1cat10n = 1   # 4
1classification = 1  # 5
_classification = 1  # 6
class = 1            # 7
Class = 1            # 8

Answer

The third, fifth, and seventh assignments are illegal. The third because it does not consist of only letters, digits, and underscores. The fifth because it starts with a digit. The seventh because it is a reserved word (fortunately, syntax hightlighting makes it stand out). While the others are legal, according to convention the sixth should be avoided because it starts with an underscore, and the second and eighth too, as they contain capitals. The eighth is the worst in this respect, as it also looks like a reserved word.

Constants

Many programming languages offer the ability to create “constants,” which are values assigned to a variable which can no longer be changed after the value has been first assigned. It is convention in most such languages that the name of a constant is written in all capitals. Constants can be useful to make code more readable. For instance, to calculate the total of a bill of €24.95 with a 15% service charge, you can use:

total = 24.95
final_total = int( 100 * total * 1.15 ) / 100
print( final_total )

However, it is more readable to write:

SERVICE_CHARGE = 1.15
CENTS = 100

total = 24.95
final_total = int( CENTS * total * SERVICE_CHARGE ) / CENTS
print( final_total )

Not only is it more readable, but it also makes the code easier to change should the service charge be calculated differently in the future. Especially if the service charge occurs in the code multiple times, if it is defined just once as a constant at the top of the code, it can be easily found and changed. When they are numerical, special values such as the service charge are often called “magic numbers,” i.e., their particular value has a special meaning, which is unclear if you just see the number, so you are better off using a meaningful name instead of the number.

While constants are very useful for coding purposes, Python does not support them (which is a pity), i.e., in the code above SERVICE_CHARGE is a regular variable and can be changed anywhere in the code. Still, it is convention that any variable that is written in all capitals is supposed to be a constant and should not be changed in the code, after it got its initial value at the top of the code.

You are encouraged to use such “all capitals variable names” whenever magic numbers occur in your code.