how to write a C-style for loop?

Dylan Moreland dylan.moreland at gmail.com
Wed Feb 15 00:44:05 EST 2006


John Salerno wrote:
> I assume this is the way for loops are written in C, but if it helps to
> be specific, I'm referring to C# for loops. The Python for loop seems to
> be the same (or similar) to C#'s foreach loop:
>
> foreach int i in X
>
> But how would you write a C# for loop in Python? Do you rework a while
> loop, or use the range() function?
>
> Here's an example:
>
> for (int i = 0; i < 50; i += 5)
>
> How would that go in Python, in the simplest and most efficient way?
>
> Thanks.

Take a look at the range() builtin. That should give you what you need.




More information about the Python-list mailing list