houbahop wrote: > Hello, > I have seen a lot of way to use the for statement, but I still don't know > how to do: > > for i=morethanzero to n > or > for i= 5 to len(var) > > of course I kno wthat I can use a while loop to achieve that but if this is > possible whith a for one, my preference goes to the for. > > regards, > Dominique.
I think the key point that hasn't been made here is what a for statement is really doing in python... >From the online documentation: "The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object" Neither of the statements: > for i=morethanzero to n > for i= 5 to len(var) create a sequence or iterable object, and that is why they don't work. That is why previous posts in this thread have suggested using range(), xrange(), etc. Because they create a sequence or iterable object. When first using python, I recall that this distinction was not clear to me, as I was used to a more traditional for statement (as in C/C++): for ( initialise ; test ; update ) Essentially, python's for statement is more like a foreach statement in many other languages (Perl, C#, etc). These statements essentially reduce the traditional form above to what many consider a more readable form: foreach item in collection: In order to transform the tradition for statement into this more readable form, each language requires that their collections being iterated over satisfy a precondition defined by the language (in python this precondition is that the collection is iterable). While this restriction may seem a little strange to people who are used to the more traditional for statement form, the result of this approach is often a more readable and clear statement. Regards, Michael Loritsch -- http://mail.python.org/mailman/listinfo/python-list