Reputation: 1305
In Java, I can use for like this:
for (index=candate*2;index<=topCandidate;index+=candidate)
I want to know how I can do this in Python?
Upvotes: 0
Views: 345
Reputation: 19329
You can use the range
function. For example:
>>> topCandidate = 20
>>> candidate = 3
>>> for i in range(candidate*2, topCandidate+1, candidate):
... print i
...
6
9
12
15
18
Upvotes: 3
Reputation: 798566
The short answer is that you wouldn't. For loops in Python iterate over an iterable, much like Java's for (:)
syntax. You can generate a numeric iterable with range()
or xrange()
, but consider using an existing iterable instead, possibly in conjunction with the functions in itertools
.
Upvotes: 5