[Tutor] working with strings in python3
Alan Gauld
alan.gauld at btinternet.com
Wed Apr 20 00:19:04 CEST 2011
"Rance Hall" <ranceh at gmail.com> wrote
> Variables are variable, that's why we call them variable.
Yes, but Programming variables(*) are variable in the sense
that they can represent different values over time. That is
not the same as requiring the values themselves to change.
> The idea of immutable strings variables blurs the
> line between these two mathematical concepts
Not at all. In math you can have a variable reference
the number 42. Later the same variable may represent
a different number, 66 say. But 66 and 42 are different,
immutable numbers. It is no different with strings in Python.
A variable can refer to 'foo'; at one point and 'bar' at another.
But 'foo' and 'bar' (and indeed 'foobar') are different values
just as 42, 66 and 108 are different numbers. Python
strings act like numbers in that regard.
> variable is variable or mutable, where a keyname can not be.
variables are always variable but only some values
are mutable You are conflating variables and values.
Strings are values (or objects if you prefer) and (arbitrarily)
chosen to be immutable by Guido. Variables are associated
with values and the values with which they are associated
can, and do, change.
(*)Note that variables in programming are treated differently
from traditional math variables in that math variables are usually
considered as symbols for a constant, unchanging value.
It is unusual in math to say
Let x = 42
.... stuff ....
Let x = 66
We would introduce a new variable for the new value.
But that idiom is very common in programming generally.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list