0

How can I write the for loop in Python as I write it in C:

for(i=0;i<10;)
{
    if(i%2==0)
       i=i+3;
    else
       i++;
    printf("%d\n",i);
}

Can anyone tell me about this? I searched a lot but couldn't find it. I wrote it like this in Python:

for i in range(0,10):
    if (i%2==0):
        i+=3
    else:
        i+=1
    print i

Output:

3
2
5
4
7
6
9
8
11
10

Expected output:

3
4
7
8
11

Can anyone also explain the reason of this output?

3
  • What output were you expecting? Commented May 12, 2015 at 10:44
  • the output looks fine Commented May 12, 2015 at 10:45
  • As i new to python I can't understand the output, please explain it to me @Ajay Commented May 12, 2015 at 10:48

3 Answers 3

5

To write the same loop in Python:

i = 0
while i < 10:
    if i % 2 == 0:
       i += 3
    else:
       i += 1
    print i

Which gives:

3
4
7
8
11

Note that, per the tutorial:

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

In a Python for loop, any changes to the loop variable (i, in this case) that occur during the loop are ignored when the loop repeats, and the next value from the object being iterated over is used. In this case, the object is a list of numbers:

>>> range(10)  # note that a 0 start is the default
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Some languages call this a for each loop. See also the language reference for more details.

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

1 Comment

I understood everything @jonrsharpe
2

range(0, 10) function returns list of values from 0 to 9:

range(0, 10) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Then for body is executed for each element in this list. If You have 0, the 0 % 2 == 0 so it prints 0 + 3 etc. In C You changed i value so You jumped to other value in set. Using python's for You will get through all elements. You should use

i = 0
while i < 10:
  if (i % 2 == 0):
    i += 3
  else:
    i += 1
  print i

To have same results as in C

1 Comment

thanks for the explanation, actually I forget the way python works, thanks a lot
0

try this

for i in range(10):
    if i%2 == 0:
        i = i+3
    else:
        i = i + 1
    print i

it gives the same output u asked for...hope this helps

3 Comments

where is the initialization?
why we can't set the steps when we use range(0,10), why we have to specifically use range(10)?
we can by range(0,10,step) but the step cannot be 0 so it wont satisfy what you wanted to achieve

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.