In Python, how does a for loop with `range` work?

前端 未结 6 1274
醉梦人生
醉梦人生 2021-01-03 19:20
for number in range(1,101): 
    print number

Can someone please explain to me why the above code prints 1-100? I understand that the range functio

6条回答
  •  既然无缘
    2021-01-03 19:39

    number is equivalent to i in your C loop, i.e., it is a variable that holds the value of each loop iteration.

    A simple translation of your Python code to C would result in something along these lines:

    for (int number = 1; number < 101; number++) {
      printf("%d\n", number);
    }
    

提交回复
热议问题