0

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.