|
Variables |
A variable is a named place in memory to store a value. We call them
variables because the values can vary (change). Suppose we are playing a game
and we want to keep track of a score. We start the game with no points, so we
might create a variable score
that begins with 0 points. We say that variable
score
is initialized to 0.
score = 0Try entering this into the Python Shell. Now there is a place in memory named "
score
" that is holding the value 0. When you see the equal sign "=
", you are
looking at a variable assignment. The value to the right of the "=
" is assigned
(placed in) the place to the left of the "=
".score
" (without the double-quotes) into the shell. Python evaluates the expression, that is, computes
what its value is. When Python evaluates a variable, it just looks in that named
place in memory and gets the value stored there. >>> score = 0 >>> score 0 >>>Now imagine that you scored a point in the game and want to add 1 to the score. Enter "
score = score + 1
", and then enter "score
". Repeat this two more times:>>> score = score + 1
>>> score
1
>>> score = score + 1
>>> score
2
>>> score = score + 1
>>> score
3
>>>
The assignment "=
" is not like mathematical equality. We are not saying that what
is on the left is equal to what is on the right. Instead, we are evaluating what is on
the right and placing it in the named location on the left. When we first typed
"score = score + 1
", Python first evaluated the expression "score + 1
" to the
right of the "=
". Since score held the value 0, this evaluated to 0 + 1, which
is 1, and 1 was assigned as the new value of score
. When we next typed "score = score + 1
", the same thing happened, except that
score
then contained 1, so that
"score + 1
" evaluated to 1 + 1, which is 2, and 2 was assigned as the new value
of score
.
A variable is created when you first assign it again. You cannot use a variable before it has been assigned. Wherever you use the variable in your program code, it is replaced by the value you have stored in the variable. To change a variable's value, just assign it again.
A variable's name should begin with a letter or an underscore "_", and all
following characters should be letters, numbers, or underscores. It
matters whether you use uppercase or lowercase characters. "SCORE
",
"Score
", and "score
" are all names of different variables. Also, you
cannot have variable names that are the same as important words in the Python
language (e.g. "if
", "while
", "import
", "return
", etc.).
There are some specialized assignments that allow common assignments to be
expressed more easily. Our "score = score + 1
" is a very
common assignment. It is so common to add, subtract, and perform other
arithmetic operations on a variable, that there is a short way of expressing
them:
>>> score += 1 # same as score = score + 1 >>> score 4 >>> score -= 2 # same as score = score - 2 >>> score 2 >>> score *= 10 # same as score = score * 10 >>> score 20 >>> score /= 5 # same as score = score / 5 >>> score 4.0 >>> score = 20 >>> score //= 5 # same as score = score // 5 (integer division) >>> score 4
There are other augmented assignment statements that you can read about here.
Each assignment is an expression that has a value. That value is the value that was assigned. This means that you can assign a number of variables to the same value at once:
>>> score1 = score2 = 0 >>> score1, score2 (0, 0)
When Python evaluates "score1 = score2 = 0
", it first seeks to
evaluate the expressions to the right of the first "=
", that is "score2
= 0
". This is an assignment, and it's value as an expression is
what was assigned, so after score2
is assigned, "score1 =
score2 = 0
" evaluates to "score1 = 0
". Placing
commas between score1
and score2
in the next line, it
evaluates to a fixed (unchangeable) list of values called a tuple.
Also, one can create a tuple that allows you to assign a number of variables in sequence. Here we see how to assign two variables at once, and how to swap the values of two variables:
>>> a, b = 1, 2 >>> a, b (1, 2) >>> a, b = b, a # swap values of a and b >>> a, b (2, 1)
As a final example, we're going to play around with variables of lists. You'll learn more about lists later, but just follow along with the description of what's happening in this interaction:
>>> list1 = [1, 2, 5] >>> list2 = list1 >>> list1 [1, 2, 5] >>> list2 [1, 2, 5] >>> list1[2] = 3 >>> list1 [1, 2, 3] >>> list2 [1, 2, 3] >>>
First, we create list with the numbers 1, 2, and 5. (Lists are
surrounded by square braces [ ].) This list is then assigned to new
variable list1
. The value that is in list1
is
then assigned to the new variable list2
. When we look at
list1
and list2
, we see the same list as expected.
The assignment "list1[2] = 3
" assigns the integer 3 to the position
that is 2 positions after the first position in the list, that is, the third
position. Here comes the surprise: When we look at what is in list1
,
we see the 5 changed to 3 in the third position. Now look at list2
.
It also has that same change! We changed list1
.
Why should that change list2
? The answer is that list1
and list2
are just different names for the same list in memory.
When we say "list1 = [1, 2, 5]
", we say list1
becomes
a name for that list we created. If we had also said "list2 = [1, 2,
5]
", then we would have created another list and had the name list2
refer to it. Instead, we said "list2 = list1
". In other
words, list2
is a name for the same thing referred to by
list1
. That's why we see two changes. Both list1
and list2
point to the same thing. Keep that in mind!
Further reference from the Python website:
© 2009 Todd Neller