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

Question:

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 function excludes the last number in the specified range, however, what is the ‘number’ part of the syntax?

I am more used to C++ & Java where I’d write the code like:

for (i = 1; i < 101; i++) {
   System.out.println(i);
   i++;
}

So what exactly is ‘number’? I’m sure i’m looking too far into this and there is a simple question.

Asked By: TopChef

||

Answers:

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("%dn", number);
}
Answered By: João Silva

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
Answered By: nevets1219

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.

Answered By: zdan

range is the list of the numbers 1 to 100.

number then references each object in that list

Answered By: sre

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.

Answered By: Smashery

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

Answered By: Dimos
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.