2-Variable For Loop in Python -


what python equivalent c/c++ code snippet below?

// rest of code. (i = 1, j = 0; < 10, j < 19; ++i, j += 2) {     // body of loop. } 

you can try this:

for i,j in zip(range(1,10),range(0,19,2)): 

you have 2 things understand:

  • how range() works
  • how zip() works

range takes 3 params. start,end , increment.

first 1 inclusive , second exclusive,3rd increment in c/c++.

range(1,10) 

as first 1 inclusive start 1, , 2nd 1 exclusive end @ 9. default increment one.

range(0,19,2) 

as wanted, loop start 0 , end @ 18 , increment 2.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -