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

前端 未结 6 1247
醉梦人生
醉梦人生 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:31

    This is a slightly confusing issue for new programmers in Python that have experience in object-oriented or procedural languages (c, Java etc.)

    The difference between those languages is that Python does not support a "counting"-like for iteration that is constantly used in C,Java etc :

    for(i = 0; i < 10; i++){
    ...
    }
    

    In contrast, Python implements only a for that is similar to the Iterator interface of object-oriented languages (Java programmers will be familiar with this) :

    for object in object_list
        ....
    

    So, in your example "range"[1,101] is the list (object_list) containing all numbers from 1 to 100 and "number" is the iterator (object) that takes the place of each one number

    0 讨论(0)
  • 2021-01-03 19:33

    range is the list of the numbers 1 to 100.

    number then references each object in that list

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2021-01-03 19:39

    As JG said, number is your variable (much like i in your C code). A for loop in python is really like a foreach loop in C# (I think Visual C++ has it too). Basically, it iterates over a container. So you can use that syntax with lists too:

    fib = [0,1,1,2,3,5,8]
    for number in fib:
        print number
    

    A range object acts sort of like a container, containing all the numbers between a and b.

    0 讨论(0)
  • 2021-01-03 19:44

    number is a variable in which each value in the range is placed.

    range actually returns an iterator, and the for is responsible for advancing it through the range.

    0 讨论(0)
  • 2021-01-03 19:50

    Python 2.7 documentation states:

    range([start], stop[, step])¶

    This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised)

    EDIT: You may also want to look at xrange. EDIT: So basically:

    for ( start ; stop ; step )
    range( start, stop, step ) // where start and step are optional
    
    0 讨论(0)
提交回复
热议问题