April 6, 2000
Something for everyone
Beginners start at the beginning, and experienced programmers jump in further along. Lesson 1 provides an overall description of this online programming course.
This lesson provides an introduction to the use of variables, and the required syntax of the identifiers used to represent variables.
A pigeonhole in memory
As a practical matter, a variable is a pigeonhole in memory, which has a nickname, where you can store values.
You can later retrieve the values that you have stored there by referring to the pigeonhole by its nickname (identifier). You can also store a different value in the pigeonhole later if you desire.
Is Python strongly typed?
One of the main differences between Python and programming languages such as Java is the concept of type.
languages like Java, variables not only have
a name, they have a type. The type determines the kind of data that
you can store in the pigeonhole.
|It is probably more correct to say that the type determines the values that you can store there and the operations (addition, subtraction, etc.) that you can perform on those values.|
Python is not strongly typed
One of the characteristics that makes Python easier to use than Java is the fact that, with Python, you don't have to be concerned about the type of a variable. Python takes care of type issues for you behind the scenes.
Declaration of variables
Another difference between Python and Java is that with Java, you must declare variables before you can use them. Declaration of variables is not required with Python.
With Python, if you need a variable, you simply come up with a name and start using it as a variable.
Dangerous curves ahead!
With this convenience comes some danger. You can only have one variable with the same name within the same scope (I will discuss scope in a subsequent lesson).
Don't use the same name for two variables
With Python, if you unintentionally use the same name for two variables, the first will be overwritten by the second. This can lead to program bugs that are difficult to find and fix.
A more subtle danger
A more subtle danger is that you create a variable that you intend to use more than once and you spell it incorrectly in one of those uses. This can be an extremely difficult problem to find and fix. I will illustrate what I mean by this later with a sample program.
Give me the rules in plain English
The notation used in the Python Language Reference to define the naming rules is a little complicated, so I will try to interpret it for you.
I believe that the Language Reference is saying that identifiers must begin with either a letter or an underscore character. Following that, you can use an unlimited sequence of letters, numbers, or underscore characters.
Case is significant
The letters can be uppercase or lowercase, and case is significant. In other words, the identifier Ax is not the same as the identifier aX.
Any old digit will do
Numbers can be any of the digit characters between and including 0 and 9.
Watch out for underscore characters
I recommend that you not use the underscore character unless you know exactly why you are using it. In some situations, the use of the underscore character has a special meaning. I will discuss some of those situations in subsequent lessons.
The first thing that you need to do is to start the interactive programming environment. If you have forgotten how to do that, see Lesson 1.
Create and use some variables
The interactive fragment shown in Figure 1:
Using the informal jargon from an earlier paragraph, two pigeonholes are established in memory and are given nicknames of x and y.
The assignment operator
Integer values of 6 and 5 are stored in the two pigeonholes using the assignment operator (=).
The use of the assignment operator in this fashion causes the value of its right operand to be stored in the pigeonhole identified by its left operand.
What is an operand?
(If you don't recognize the use of the term operand, see an earlier lesson for an explanation.)
In this case, the right operands of the two variables are literal numeric values.
The left operands of the two variables are the nicknames identifying the two memory locations that constitute the variables named x and y.
Addition of variables
Then, in the third line of code, the values are retrieved from each pigeonhole and added together. The result of the addition (11) is displayed as output from the expression x+y.
Assigning the same value to several variables
Python allows you to assign the same value to several variables, causing them to come into existence (begin to occupy memory) at the same time if necessary
Consider the interactive fragment shown in Figure 2.
Create three variables
The first statement creates three variables named a, b, and c, and assigns a value of 10 to each of them.
They are then added together, in the second line of code, to produce an output value of 30.
Assign different values
The boldface statement in the fourth line assigns the value of 20 to the same three variables, replacing what was previously there with the new value. Again they are added together, this time producing an output value of 60.
In most modern programming systems, values having fractional parts, such as 3.14159 are commonly referred to as floating point types. (This terminology comes from the fact that the decimal point can float back and forth from left to right.)
Similarly, whole number values are commonly referred to as integer types. (These are values with no decimal parts, such as, "I have 3 whole apples.")
Advantages and disadvantages
Each type has advantages and disadvantages relative to the other.
The range of values
For example, in some systems, the total range of values for an integer type is restricted to the set of whole numbers between Ė32768 and +32767. Anything outside that range cannot be handled as a whole number.
Although the range of an integer type will be different on different systems, it will almost always be less than the range of a floating point type on the same system.
However, on some systems integer arithmetic is performed much faster than floating point arithmetic. On those systems, if speed is important, using integers may be more attractive than using floating point types.
Floating point provides greater range
On most systems, the floating point type provides a much greater range in terms of the values that can be maintained and used for arithmetic. For example, a particular system might be capable of representing the following two values as well as millions of values in between:
Sometimes range is important, and sometimes not
Sometimes range is important, and sometimes it isn't. However, as I mentioned above, in many cases this greater range is obtained at some sacrifice in arithmetic speed relative to integer types.
Also, as I will explain in the Review section, floating point arithmetic often produces approximate results instead of exact results.
While approximate results might he OK for scientific calculations, they might not be OK for financial calculations.
Automatic type handling in Python
In strongly-typed languages such as Java, it is the responsibility of the programmer to make certain that types are handled correctly. For example, it is often not possible to store a floating point value into a variable previously declared to be for the storage of integer values. There is a very strong possibility that it simply won't fit.
Python takes care of the routine type issues for us automatically.
Consider the interactive code fragment shown in Figure 3. The variables x and y are originally created to store integers and are populated with the values 5 and 6 respectively. The variables are added and the correct sum is displayed as output from the interpreter.
Next assign some floating point values
Then the floating point values 5.55555 and 6.66666 are assigned to the same two variables named x and y. The two variables are successfully added and the correct result is displayed, demonstrating that the two floating point values were successfully stored in the variables originally created for integers.
How is this accomplished?
I don't know how this is accomplished. As Python programmers, we don't really care. We are simply happy that it works without the requirement for us to deal with the details of type.
As I mentioned in an earlier lesson, you might want to be very careful when doing division. If both operands are integers, you will not get the floating point result that you might be hoping for. The quotient will be truncated down to the next (algebraically) lower integer. To get a floating point result from a division, one of the operands must be a floating point value.
The magic continuation variable
In interactive mode, Python automatically provides a variable whose name is simply the underscore character (_).
This variable makes it easy to do continuation arithmetic in interactive mode. (This variable is intended for read only purposes, so don't assign a value to it explicitly.)
At any point in time, this variable will contain the most recent output value displayed by the interpreter.
How does it work?
Consider the interactive code fragment shown in Figure 4. This fragment starts out just like previous examples, causing the sum of 5 and 6 to be calculated and displayed.
Sum is saved in the continuation variable
As mentioned above, the sum value of 11 is automatically saved in the continuation variable whose name is simply the underscore.
The contents of the continuation variable (11) are then added to 22 producing a result of 33 (note the use of the underscore as a variable name in the boldface expression).
Tell me why again
The primary purpose of this automatic variable named _ is to make it easier for you to string calculations together in interactive mode and to display the intermediate results as you go.
Illegal variable names
The interactive fragment in Figure 5 shows the result of attempting to use an illegal variable name.
(As I indicated earlier, variable names cannot begin with a digit. They must begin with either a letter or an underscore character)
The result shown in Figure 5 is generally self-explanatory. The little pointer points to the x in the intended variable name, 1x, reporting that this is a syntax error.
Variable name spelling errors
The interpreter assumes that you know what you are doing, and won't help you avoid spelling errors in variable names (unless the spelling error produces an illegal variable name).
A serious potential programming problem
Now I will illustrate a very subtle and serious potential problem. Consider the interactive code fragment in Figure 6. The programmer expected to get a final answer of 16+5 = 21, but instead the final answer was 11.
Why did this happen?
The problem arose in the line with the boldface highlight. In this line, the programmer intended to assign a value of 16 to the existing variable named xypdq.
However, because of a spelling error, the programmer created a new variable named xyppq and assigned the new value of 16 to the new variable instead of assigning it to the existing variable.
As a result, the value stored in the original variable wasn't changed, and when that variable was used later in an expression, the result did not meet the programmer's expectations.
Spelling errors can be dangerous
This is one of the greatest dangers of using a programming language that doesn't require the declaration of variables. This type of spelling error is easy to make (as a result of a simple typing error), and can be extremely difficult to find and fix.
Defending against spelling errors
The best defense against this kind of error is to make all of your variable names meaningful. Then if you make a typing error (that results in a spelling error), you might have a better chance of finding it later.
Meaningful variable names
Some meaningful variable names follow. Note the judicious use of upper and lower case to visually break up the variable name into separate words. This is a naming convention that has become very popular, particular among Java programmers.
The variable named MyUpperLimit is not the same variable as the one named myUpperLimit.
As a practical matter, it is poor programming practice do distinguish between two variable names simply by using subtle differences in case. This will almost surely lead to spelling errors later.
Ans: False. The value of a variable is intended to change during the execution of the program. The value of a constant (which I haven't discussed yet) is not intended to change.
2. Python is a strongly typed language, True or False.
Ans: False. Python is not a strongly typed language. From a pure technical viewpoint, the Python programmer rarely needs to be concerned about type.
3. Python programmers must declare all variables, True or False?
Ans: False. Variable declarations are not required in Python. All that is required to cause a variable to come into existence is to invent a new name for a variable and assign a value to it.
4. Explain the dangers of using a language that does not require variables to be declared.
Ans: Several different kinds of problems can result from making typing errors that result in misspelling the names of variables. These errors usually result in programs that produce incorrect results without warning.
5. What is the best defense against spelling errors in variables names?
Ans: Use meaningful variable names for which the spelling is obvious, such as theOverheadRate.
6. Variable names can begin with the digit characters, True or False?
Ans: False. Variable names must begin with a letter or underscore character.
7. The underscore character should be used liberally in variable names, True or False?
Ans: False. You should use the underscore character in a variable name only when you know exactly why you are using it. Otherwise, you may create conflicts with special system variables whose names contain underscore characters.
8. Write a simple program that illustrates case sensitivity in the names of variables.
Ans: An example of such a program is shown in Figure 7. Note that the names of the two variables have the same letters, but different case. The fact that the two variables are different variables is illustrated by the fact that each is assigned a different value. The sum of the two variables demonstrates that the two variables contain different, and correct, values.
Contrast the above result with the program in Figure 8 where a variable whose name contains the same letters and the same case is used.
All this program accomplishes is the assignment of two different values to the same variable. It then adds the variable to itself using its current value of 20 producing a result of 40 (instead of 30 as in the previous example).
9. Explain the use of the assignment operator.
Ans: The assignment operator causes the value of its right operand to be stored in the memory location identified by its left operand.
10. Which type usually provides the greater range for storage of numeric values, integer or floating point?
Ans: Floating point usually provides the greater range for storage of numeric values.
11. Should you just always use floating point instead of integer to be safe?
Ans: Probably not. Floating point arithmetic often suffers from speed penalties. In addition, integer arithmetic produces exact results while floating point arithmetic usually produces approximate results (although the approximations may be very close to being exact in many cases).
12. Write a simple program that illustrates the approximation nature of floating point arithmetic.
Ans: See the sample program in Figure 9. We all know that the true result of this expression is an unending string of nines, as in 9.999999999999999...
However, in this case, Python returned an answer of 10.0, indicating that the answer is accurate to three significant figures. This is not really the correct answer, but as a practical matter, it may be the best answer. I will leave that for you to decide.
13. Explain the purpose of the automatic continuation variable whose name is simply the underscore character.
Ans: The primary purpose of the automatic variable named
_ is to make it easier for you to string calculations together in interactive
mode and to display the intermediate results as you go.
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.