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

Alan Gauld alan.gauld at btinternet.com
Tue Apr 3 19:27:17 CEST 2012


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?

In Python variables are defined by using them.

In the code below you have i used in a for loop, even though not 
mentioned before. j is similarly being used in the generator expression 
for loop:

choice(lc) for j in range (dlen)

unwraps to:

dummy = []
for j in range(dlen):
    dummy.append(choice(lc))

Which effectively creates a list of dlen choice items.


> from random import randrange, choice
> from string import ascii_lowercase as lc
> from sys import maxsize
> from time import ctime
>
> tlds = ('com', 'edu', 'net', 'org', 'gov')
>
> for i in range(randrange(5,11)):
>      dtint=randrange(maxsize)    #pick a random number to use to

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list