for loop conversion from C to Python -
how can write for loop in python write in c:
for(i=0;i<10;) { if(i%2==0) i=i+3; else i++; printf("%d\n",i); } can tell me this? searched lot couldn't find it. wrote in python:
for in range(0,10): if (i%2==0): i+=3 else: i+=1 print output:
3 2 5 4 7 6 9 8 11 10 expected output:
3 4 7 8 11 can explain reason of output?
to write same loop in python:
i = 0 while < 10: if % 2 == 0: += 3 else: += 1 print which gives:
3 4 7 8 11 note that, per the tutorial:
the
forstatement in python differs bit may used in c or pascal. rather iterating on arithmetic progression of numbers (like in pascal), or giving user ability define both iteration step , halting condition (as c), python’sforstatement iterates on items of sequence (a list or string), in order appear in sequence.
in python for loop, changes loop variable (i, in case) occur during loop ignored when loop repeats, , next value object being iterated on used. in case, object list of numbers:
>>> range(10) # note 0 start default [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] some languages call for each loop. see the language reference more details.
Comments
Post a Comment