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 code).
for
with range
doesn't give me the flexibility of an actual for loop.
for in Loop: For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages.
The bash C-style for loop share a common heritage with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3).
For loop in python is used to iterate over either a list, a tuple, a dictionary, a set, or a string. It allows us to efficiently write a loop that needs to execute a specific number of times. For loop is initialized using for keyword. For loop in C is a entry-cotrolled loop.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With