[Tutor] for loops ... (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Nov 18 13:06:28 EST 2003


> 1.) yet on the beggining I had problem understanding, or better imaging
> while loop,
>
> >>> j = 1
> >>> while j <= 12:
> ...         print "%d x 12 = %d" % (j, j*12)
> ...         j = j + 1
>
> ... the part, where it says j = j + 1 ... looked at first a bit
> illogical, like 1 = 1 + 1, so 1 = 2 come after,so this equation seems
> illogical (the value on both sides of = sign should be identical - that
> is the deffinition of equation).


Right --- the symbol '=' in many programming languages does not mean
"mathematical equality", but instead, it's meant to symbolize
"assignment".


It's one of the major places where programming deviates from standard
mathematics: instead of declaring a certain relation, we often have to
instruct the computer to take an action.

When we say:

    j = j + 1

imagine seeing a large arrow point to the left, like:

    j <------------ j + 1

The expression 'j + 1' is being assigned back into 'j'.



> So I tryed to imagine it not as equation, but rather as some
> "instructon" how number (set by variable should "change/increase"
> through "looping" cicles.
>
> So I tryed to type j = 1 then j = j + 1, and of course after print j it
> says j = 2, so in case above (if j = 1) means variable j changes from 1
> to 2, and then from 2 to 3, every loop +1, and so on to 11 ... is that
> it ??


Yes, exactly: when we're programming in Python, we're not really writing
math equations.




> So please, give me some background releted to that kind of
> equations/instructions (j = j + 1), how they "control" loops, which has
> higher "priority" - for better imaging !!


There are a few tutorials that should help introduce these ideas:

    http://python.org/topics/learn/non-prog.html

Try going through one of them first, and then ask your questions again.
*grin*



> 2.)  Here it is something different, though it is again while loop with
> that kind of "equation", but ...
>
> I don't get it, how it print that high values as a result.
>
> I thought, that it first "takes" dog value 2, defined in tadey(2), then
> "calculates" current value for this loop, so dog = dog - 1, that gives
> 1, than multiplicate with result, which is set 1, which gives the real
> result ... but as you see (if you calculate in my way), I didn't get the
> right values.
>
> >>> def tadey(glg):
>             result = 1
>             while glg > 0:
>                    result = result * glg
>                    glg = glg - 1
>             return result



Very close.  Let's simulate tadey(3), step by step, and see what happens.
There are two variables that are in motion here, 'result' and 'glg'.



At the very start, we have something like:

    result = ?, glg = 3

I'm putting a question mark here because we're at the very beginning, and
I have no idea what result should be.  Let's do the first line:

>             result = 1


Now we have:

    result = 1, glg = 3


We then reach a while loop:

>             while glg > 0:
>                    result = result * glg
>                    glg = glg - 1

Since glg is still greater than 0, we'll repeat the body of this loop till
glg is no longer greater than zero.

>                    result = result * glg


Now we have:

     result = 3, glg = 3


>                    glg = glg - 1


     result = 3, glg = 2


>                    result = result * glg


     result = 6, glg = 2


>                    glg = glg - 1


     result = 6, glg = 1


We still have to execute the loop body one more time, since glg is still
greater than zero!


>                    result = result * glg


    result = 6, glg = 1

>                    glg = glg - 1

    result = 6, glg = 0



And now we come out of the while loop.  But we still have one more
statement to execute:


>             return result


And we return 'result' as our answer.  tadey(3) == 6.






Out of curiosity, would something like this make more sense for you?

###
def tadey(glg):
    if tadey <= 0: return 1
    return glg * tadey(glg-1)
###

If not, don't worry about it; I'm just curious to see if this "recursive"
definition makes more sense than the loopy version of the code.


Hope this helps!




More information about the Tutor mailing list