FOR Loops List Comprehensions
<p>Feb 13, 2013 07:41 PM
 
section 4.2 FOR statements... actually look at 4.4  PRIME NUMBER example!!
 
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
</pre>
<p> 
tonite this drove me nucking futz!
 
note:  for loop normally takes a : with the conditional above
HOWEVER it does NOT take a COLON in a  LIST COMPREHENSION!
 
see examples at 5.  these work:
 
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers = [i for i in range(10) if i>3]
>>> numbers
[4, 5, 6, 7, 8, 9]  and this works
 
>>> numbers = [i for i in range(10) if i%2==1]
>>> numbers
[1, 3, 5, 7, 9]
 
Note:  do not put an indentation in a list comprehension... it won't work!non
 
 
 </p>