[Tutor] Bug in python, or is it just 3am

Alan Gauld alan.gauld at freenet.co.uk
Fri Apr 21 12:30:59 CEST 2006


> But when i use a number = number + 1
> right after the value stays the same,

I'm not sure what you mean by that.

> Now i thought that number = number + 1 just wasn't
> vailed in python untill i tried it again and it
> worked,

variable = variable + 1

is perfectly valid. It is not the normal math meaning of an equation 
however, it is an assignment statement. In a language like Smalltalk 
or Pascal they use a different symbol (:=) for assignment which 
is IMHO A Good Thing(TM)  And they traditionally read that 
symbol as "becomes", thus:

variable := variable + 1

is read: "variable becomes variable plus one"

What it means is that variable takes on the previous value 
of variable plus one. So if it starts as 42 it ends as 43

This is such a common thing to do that Python actually 
has a shorthand for it:

variable += 1

And after all that, I've just realised that I don't discuss this 
at all in my tutorial so I need to add an explanation this weekend. 
So thanks for asking the question! :-)

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list