[Tutor] Question about login=''.join(choice(lc) for j in range(llen))

wesley chun wescpy at gmail.com
Tue Apr 3 22:40:36 CEST 2012


On Tue, Apr 3, 2012 at 10:50 AM, Peter Otten <__peter__ at web.de> wrote:
> Alan Gauld wrote:
>> On 03/04/12 15:54, Khalid Al-Ghamdi wrote:
>>
>>>      dom="".join(choice(lc) for j in range (dlen))
>>>
>>> how does the interpreter know what "j" is supposed to refer to when it
>>> was not mentioned prior?


+1 everyone else's replies so far. i'll add the following: you create
variables by assigning things to them. in this example, no prior code
used the variable 'x':

>>> x = 10
>>> print x
10

similarly, when used in a for-loop, it's like you had an "invisible"
assignment at the "top" of the loop. here's an example:

>>> for i in range(5):
...  print i
...
0
1
2
3
4
>>> print i
4

notice that the 'i' variable is still there even after the loop has
ended. it's as if you did the following:

>>> i = 0
>>> print i
0
>>> i = 1
    :
>>> i = 4
>>> print i  # 1st time, part of the "loop"
4
>>> print i  # 2nd time, "outside" of the loop
4

hope this helps!
--wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
    wesley chun : wescpy at gmail : @wescpy/+wescpy
    Python training & consulting : CyberwebConsulting.com
    "Core Python" books : CorePython.com
    Python blog: wescpy.blogspot.com


More information about the Tutor mailing list