Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   loops (http://www.programmingforums.org/showthread.php?t=13081)

mark Apr 29th, 2007 8:46 PM

loops
 
hello im trying to figure out how to specify which code is part of the loop and which is not, on the tutorial im reading it says to indent the code, but is there any other better way to do it.

titaniumdecoy Apr 29th, 2007 8:47 PM

No. In Python, code blocks are defined by their indentation.

Fred May 1st, 2007 2:11 PM

As far as I know it is the only way...
That's just python. There is no 'end of loop' statement!
U' get used to it!

Dietrich May 1st, 2007 3:30 PM

If your statement block is short, you can do it all on one line:
:

for x in range(1, 11): x *=3; print x
Honestly, the indented way is much more readable!

Hi Fred!

ptmcg May 1st, 2007 6:25 PM

If you are looping for the purpose of building a list, read up on list comprehensions. Here is a quick way to build a list of the odd numbers up to 20:

:

print [c for c in range(20) if c%2 ]


All times are GMT -5. The time now is 2:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC