Quantcast
Channel: How do I use a C-style for loop in Python? - Stack Overflow
Browsing index pages (12 articles)
↧

Answer by Ed Avis for How do I use a C-style for loop in Python?

Perhaps I may give another motivating example: if the size of the list may change during the loop. The token gobble replaces itself and the next two tokens with gulp.lines = ['a', 'gobble', 'c', 'd',...

View Article


How do I use a C-style for loop in Python?

I want to use the traditional C-style for loop in Python. I want to loop through characters of a string, but also know what it is, and be able to jump through characters (e.g. i =5 somewhere in the...

View Article


Answer by Taymon for How do I use a C-style for loop in Python?

The Python for loop always has foreach semantics. You can, however, do this:for i in xrange(10): print iThis is very much like a C for loop. xrange (or range, as it was renamed in Python 3) is a...

View Article

Answer by Adonais for How do I use a C-style for loop in Python?

for i in range(n):...is the Python equivalent of the C...for (i = 0; i < n; i++){Or well, you can use:for i in range(a, n, s):...which is equivalent to...for (i = a; i < n; i+=s){

View Article

Answer by Jeffrey for How do I use a C-style for loop in Python?

You can do the following, given an array a:for i in range(len(a)): a[i] = iThat's the closest Python can get to C-style loops. You can also give the range command more arguments; for example,for i in...

View Article


Answer by kev for How do I use a C-style for loop in Python?

In C:for(int i=0; i<9; i+=2){ dosomething(i);}In python3:for i in range(0, 9, 2): dosomething(i)You just express the same idea in different languages.

View Article

Answer by Karl Knechtel for How do I use a C-style for loop in Python?

I provide the following entirely facetious solution by way of protest. Note that 'break' and 'continue' will not work. Also note that the loop body must not be indented.class For: def __init__(self,...

View Article

Answer by rob mayoff for How do I use a C-style for loop in Python?

There is no simple, precise equivalent of C's for statement in Python. Other answers cover using a Python for statement with a range, and that is absolutely what you should do when possible.If you want...

View Article


Answer by user2492477 for How do I use a C-style for loop in Python?

For all the ones going "Why?", here's an example where a C-style loop would be helpful:remaining_retries = 3for i in range(num_runs): success = do_run() if not success and remaining_retries > 0: i =...

View Article


Answer by Chris for How do I use a C-style for loop in Python?

The top answer here is fundamentally incorrect in any true for-looping situation. And the second answer does address the issue, but over complicates things (a C-loop is possible).Bottom Line, Up Front...

View Article

Answer by Ethan Hunter for How do I use a C-style for loop in Python?

Since Python 3.8, we have the assignment expression operator or "walrus" operator, :=. Its use is just what it sounds like, turning assignments into expressions, returning the assigned value like...

View Article

Answer by user1537366 for How do I use a C-style for loop in Python?

An idea is to have a generator that yields an object, in which lies the loop variable. That way, you can have both the original generator and the loop accessing the same loop variable. Naturally, this...

View Article
Browsing index pages (12 articles)


Latest Images