How to do loop like: for(i = 0; i < 100; i++)?
This code is in PHP, i want create this loop like in Python 3x.
Thanks my brothers for any what you do for me.
How to do loop like: for(i = 0; i < 100; i++)?
This code is in PHP, i want create this loop like in Python 3x.
Thanks my brothers for any what you do for me.
Use the built-in range function which takes an optional step and an interval [a, b) like so:
for i in range(0, 100, 1):
# do something
A range based for loop in python is not equivalent to C like for loop that is prevalent in javascript, php or languages as such which supports them. Particularly, the distinction comes in
Ideally the best alternative is to re-write the for loop to a while loop in the similar way as you would have written in languages that supports both. For example
for(<initializer>; <cond>; <increment>) {
// Loop Body
}
should equivalently be written as
<initializer>
while cond:
<Loop Body>
<increment>
so in your particular case, the equivalent of the php for(i = 0; i < 100; i++) construct in python would be
i = 0
while i < 100:
# Loop Body
i += 1
Using any other construct might have some surprising consequences.
foreach loop, that most modern languages supports to a varied way.