[Edu-sig] python for kids...
Jason R Briggs
jasonrbriggs at gmail.com
Sat Nov 10 23:46:54 CET 2007
Hi All
I'm seeking some advice, about a change I'm thinking about making to
Chapter 2 of my book. I've received a bit of criticism lately about my
decision to stick with the simple concept of a variable as a "box to put
things in", rather than explaining the difference in Python (that a
variable is a really a name or label for an object).
My original thinking was to keep it simple (not go into a discussion of
store-by-value versus store-by-reference), but I've gradually been
convinced, by weight of opinion, that this wasn't the right decision.
When you get an email from the Associate Professor of Computer Science
at a Brazilian university, you tend to listen more than when you get a
mail from someone saying, "dude, you're an idiot, don't you know how
Python works!". ;-)
Anyway, I'm thinking to keep the original discussion about a variable
being a mailbox, and then talk about the Python distinction (see updated
version below), but I'm not sure that this won't just unnecessarily
confuse the reader. I can scrap the mailbox stuff, but then I lose an
illustration (something I'm loathe to do anyway), plus I think in some
ways that it's useful information.
Any other opinions on the matter?
Thanks,
Jason
--- excerpt ---
A "variable" is a programming term used to describe a place to store
things. The "things" can be numbers, or text, or lists of numbers and
text -- and all sorts of other items too numerous to go into here. For
the moment, let's just think of a variable as something a bit like a
mailbox.
You can put things (such as a letter or a package) in a mailbox, just as
you can put things (numbers, text, lists of numbers and text, etc, etc,
etc) in a variable. This mailbox idea is the way many programming
languages work.
In Python, variables are slightly different. Rather than being a box
with things in it, a variable is more like a label which is stuck on the
things. We can pull that label off and stick it on something else, or
even stick the label on more than one thing.
We create a variable by giving it a name, using an equals sign (=), then
telling Python what we want that name to point at. For example:
>>> fred = 100
We've just created a variable called "fred" and said that it points to
the number 100. It's a bit like telling Python to remember that number
because we want to use it later. To find out what a variable is pointing
at, we can just type "print" in the console, followed by the variable
name, and hit the Enter key. For example:
>>> print fred
100
We can also tell Python we want the variable fred to point at something
else:
>>> fred = 200
>>> print fred
200
On the first line we say we now want to fred to point at the number 200.
Then in the second line we ask what fred is pointing, at just to prove
it changed.
We can also point more than one variable at the same thing:
>>> john = fred
>>> print john
200
In the code above, we're saying that we want john to point at the same
thing fred is pointing to.
Of course, "fred" isn't a very useful name for a variable. It doesn't
tell us anything about what it's used for. A mailbox is easy -- you use
a mailbox for mail. But a variable can have a number of different uses,
and can point at a whole bunch of different things, so we usually want
something more informative to describe it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/edu-sig/attachments/20071111/4aa72129/attachment.htm
More information about the Edu-sig
mailing list