remy
remy

Reputation: 1305

Python: for loop expression

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

Answers (2)

srgerg
srgerg

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

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

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

Related Questions