User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,466 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,776 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 2103 | Replies: 4
Reply
Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

newbee: simple python for loop question

  #1  
Sep 12th, 2007
test = [random.randint(1,10) for i in range(20)]

Can someone what does the random.randint(1,10) fron to for loop does, and the square brackets around it means, make it a list?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 27
Reputation: StrikerX11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
StrikerX11 StrikerX11 is offline Offline
Light Poster

Re: newbee: simple python for loop question

  #2  
Sep 13th, 2007
 test = [random.randint(1,10) for i in range(20)]

equals
>>> import random
>>> test=[]
>>> for i in range(20):
    #test.append(random.randint(1, 10))
    random_int=random.randint(1, 10)
    test.append(random_int)
Last edited by StrikerX11 : Sep 13th, 2007 at 12:28 am.
Reply With Quote  
Join Date: Aug 2007
Location: Johannesburg, South Africa
Posts: 6
Reputation: grobister is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
grobister's Avatar
grobister grobister is offline Offline
Newbie Poster

Re: newbee: simple python for loop question

  #3  
Sep 13th, 2007
It should give you a list of 20 random numbers between 1 and 10

e.g. [3,6,7,1,9.......]

It's not really written the "python way" which encourages transparent readable code!

hth

:-D
--
post mortem nulla voluptas
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: newbee: simple python for loop question

  #4  
Sep 13th, 2007
The square brackets are part of a construction called a 'list comprehension'. The basic syntax is

[x for blah in yourlist]

The idea is to create a list quickly in one line. And as StrikerX11 noted, it's exactly the same as

  1. tmp = []
  2. for blah in yourlist:
  3. tmp.append(x)

(Usually, x and blah have some relationship to each other, but as you can see in this case, not always).

Originally Posted by grobister
It's not really written the "python way" which encourages transparent readable code!

I wonder what the clearest way to write that would be. I found myself saying, "That's how I'd do it..."

Jeff
Reply With Quote  
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: newbee: simple python for loop question

  #5  
Sep 13th, 2007
Once you get familiar with constructs like list comprehension, they become quite easy to read. List comprehensions also give quite an improvement in speed too.
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 2:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC