Precedence and Associativity Top Assignment Contents

Variables

Suppose you found an envelope lying on the street and on the front of the envelope was printed the name numberOfDogsTeeth. Suppose further that you opened the envelope and inside was a piece of paper with the number 42 written upon it. What might you conclude from such an encounter? Now suppose you kept walking and found another envelope labeled meaningOfLifeUniverseEverything and, again, upon opening it you found a slip of paper with the number 42 on it. Further down the road, you find two more envelopes, entitled numberOfDotsOnPairOfDice and StatuteOfLibertyArmLength, both of which contain the number 42.

Finally, you find one last envelope labeled sixTimesNine and inside of it you, yet again, find the number 42. At this point, you're probably thinking "somebody has an odd affection for the number 42" but then the times table that is stuck somewhere in the dim recesses of your brain begins yelling at you saying "54! It's 54!". After this goes on for an embarrassingly long time, you realize that 6 * 9 is not 42, but 54. So you cross out the 42 in the last envelope and write 54 instead and put the envelope back where you found it.

This strange little story, believe it or not, has profound implications for writing programs that both humans and computers can understand. For programming languages, the envelope is a metaphor for something called a variable, which can be thought of as a label for a place in memory where a literal value can reside. In other words, a variable can be thought of as a convenient name for a value. In many programming languages, one can change the value at that memory location, much like replacing the contents of an envelope.4 A variable is our first encounter with a concept known as abstraction, a concept that is fundamental to the whole of computer science.5

Variables

Most likely, you've encountered the term variable before. Consider the slope-intercept form of an algebraic equation of a particular line:

y = 2x - 3

You probably can tell from this equation that the slope of this line is 2 and that it intercepts the y-axis at -3. But what role do the letters y and x actually play? The names x and y are placeholders and stand for the x- and y-coordinates of any conceivable point on that line. Without placeholders, the line would have to be described by listing every point on the line. Since there are an infinite number of points, clearly an exhaustive list is not feasible. As you learned in your algebra class, the common name for a place holder for a specific value is the term variable.

One can generalize the above line resulting in an equation that describes every line.6

y = mx + b

Here, the variable m stands for the slope and b stands for the y-intercept. Clearly, this equation was not dreamed up by an English-speaking computer scientist; a cardinal rule is to choose good names or mnemonics for variables, such as s for slope and i for intercept. But alas, for historical reasons, we are stuck with m and b.

The term variable is also used in most programming languages, including Python, and the term has roughly the equivalent meaning. The difference is programming languages use the envelope metaphor while algebraic meaning of variable is an equivalence to a value.7 The difference is purely philosophical and not worth going into at this time. Suppose you found three envelopes, marked m, x, and b, and inside those three envelopes you found the numbers 6, 9, and -12 respectively. If you were asked to make a y envelope, what number should you put inside? If the number 42 in the sixTimesNine envelope in the previous story did not bother you (e.g., your internal times table was nowhere to be found), perhaps you might need a little help in completing your task. We can have Python calculate this number with the following dialog:

    >>> m = 6
    
    >>> x = 9  
    
    >>> b = -12
    
    >>> y = m * x + b
    
    >>> y
    42

The Python interpreter, when asked to compute the value of an expression containing variables, goes to those envelopes (so to speak) and retrieves the values stored there. Note also that Python requires the use of the multiplication sign to multiply the slope m by the x value. In the algebraic equation, the multiplication sign is elided, but is required here.

One creates variables in Python by simply assigning a value to the variable.8. If the variable does not exist, it is created; if it does exist, it's value is updated. Note that the interpreter does not give a response when a variable is created or updated.

Here are some more examples of variable creation:

    >>> dots = 42

    >>> bones = 206

    >>> dots
    42

    >>> bones
    206

    >>> CLXIV = bones - dots
    164

After a variable is created/updated, the variable and its value can be used interchangeably. Thus, one use of variables is to set up constants that will be used over and over again. For example, it is an easy matter to set up an equivalence between the variable PI and the real number 3.14159.

    PI = 3.14159
    radius = 10
    area = PI * radius * radius
    circumference = 2 * PI * radius

Notice how the expressions used to compute the values of the variables area and circumference are more readable than if 3.14159 was used instead of PI. In fact, that is one of the main uses of variables, to make code more readable. The second is if the value of PI should change (e.g. a more accurate value of PI is desired,9 we would only need to change the definition of PI (this assumes, of course, we can store those definitions for later retrieval and do not need to type them into the interpreter again).

Variable naming

Like many languages, Python is quite restrictive in regards to legal variable names. A variable name must begin with a letter or an underscore and may be followed by any number of letters, digits, or underscores.

Variables are the next layer in a programming languages, resting on the literal expressions and combinations of expressions (which are expressions themselves). In fact, variables can be thought of as an abstraction of the literals and collections of literals. As an analogy, consider your name. Your name is not you, but it is a convenient (and abstract) way of referring to you. In the same way, variables can be considered as the names of things. A variable isn't the thing itself, but a convenient way to referring to the thing.

While Python lets you name variables in wild ways:

    >>> _1_2_3_iiiiii__ = 7

you should temper your creativity if it gets out of hand. For example, rather than use the variable m for the slope, we could use the name slope instead:

    slope = 6

We could have also used a different name:

    _e_p_o_l_s_ = 6

The name _e_p_o_l_s_ is a perfectly good variable name from Python's point of view. It is a particularly poor name from the point of making your Python programs readable by you and others. It is important that your variable names reflect their purpose. In the example above, which is the better name: b, i, intercept, or _t_p_e_c_r_e_t_n_i_ to represent the intercept of a line?

lusth@cs.ua.edu


Precedence and Associativity Top Assignment Contents