[Tutor] finding factorials - hmm..., gcd
glingl
glingl@mail.rg16.asn-wien.ac.at
Wed Jul 2 03:15:03 2003
--- Payal Rathod <payal-python@staticky.com> schrieb:
..
> Commented example below. Print statements are now numbered.
>
> print "WHAT'S GOING ON?"
> x =4
> y=10
> z = 0
> print "x =",x,"y =",y,"z =",z # -------> (1)
>
> def look(a,b):
> print "x =",x,"y =",y,"z =",z,"a =",a,"b =",b # --------> (2)
> a=2*a
> b=2*b
> c=a*b
> print "x =",x,"y =",y,"z =",z,"a =",a,"b =",b,"c =",c # ------> (3)
> return c
> c = c*c # ;-)
> print "x =",x,"y =",y,"z =",z,"a =",a,"b =",b,"c =",c # -----> (4)
>
> print "x =",x,"y =",y,"z =",z # ------------> (5)
> z = look(x,y) # this will execute 2 (!) print-statements
> # you won't be able to print a,b,c here
> print "x =",x,"y =",y,"z =",z # ---------> (6)
>
>
> The output is,
>
> WHAT'S GOING ON?
> x = 4 y = 10 z = 0 # This comes from print statement (1)
right!
> x = 4 y = 10 z = 0 # This comes from print statement (5)
right!
> x = 4 y = 10 z = 0 a = 4 b = 10
> # Where the world this comes from?
from print statement (2), which gets executed whenever the function
look is called
> # a and b are never defined anywhere,
the are! in the first line of the definition of look:
def look(a,b)
a and b get created, every time you call look - an destroyed
every time the execution of look has ceased.
> # nor are they assigned values of x and y.
They are! When look(x,y) is called, a gets the value of x
and b gets the value of y. (Think of: a and b are substituted
by x and y). You can substitute other values, e.g. by calling
look(1001,-1)
> # Where did python get the
> # idea that a = 4 and b = 10?
When look(x,y) is called, a gets the value of x
and b gets the value of y. (Think of: a and b are substituted
by x and y). You can substitute other values, e.g. by calling
look(1001,-1)
> x = 4 y = 10 z = 0 a = 8 b = 20 c = 160
While the values of a and b change during the execution of look,
the variables x and y retain their old values.
> x = 4 y = 10 z = 160
> # Once the above question is answered I think this will have the same
> # logic.
I've tried my best
Regards, Gregor
>
> Thanks a lot for this "magic" example.
> With warm regards,
> -Payal
>
> --
> "Visit GNU/Linux Success Stories"
> http://payal.staticky.com
> Guest-Book Section Updated.
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>