I know this is not at all central to your book, but perhaps a one-line example here would be useful. Something like:
E.g., "
for i in range(10):
" would be the equivalent of the Java expression "for (int i = 0; i I know this is not at all central to your book, but perhaps a one-line example here would be useful. Something like:
E.g., "
for i in range(10):
" would be the equivalent of the Java expression "for (int i = 0; i
Well that didn't come out well...
Example again:
for i in range(10):
# doSomething
equivalent to
for (int i = 0; i Well that didn't come out well...
Example again:
for i in range(10):
# doSomething
equivalent to
for (int i = 0; i
Okay, it's not me. It's the formatter. You know what I mean, right?
I'm a Python newbie but do know that for very large loops, xrange() rather than range() is your friend, so maybe you might want to change the reference from "range()" to "range() or xrange()".
I agree. A simple example of range() would be useful here.
Strictly speaking, the C for statement doesn't require a numeric iteration, and so is much more powerful than using a range. For example
for (node=head; node != NULL; node=node->next)...
Of course, given that this feature of C is most often used for iterating through a list, which Python does directly, perhaps this isn't a significant complaint.
With the advent of "iterator" in Python 2.2 the preceding example in C is expressed clearer in Python: the
for node in iterable:
pass # some statements
is equivalent to:
_temp_iter = iter(c)
while True:
try: x = _temp_iter.next()
except StopIteration: break
pass # some statements
enumerate could be mentioned