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

Clash Royale CLAN TAG#URR8PPP
In Python, how does a for loop with `range` work?
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.
i++
return
6 Answers
6
number is equivalent to i in your C loop, i.e., it is a variable that holds the value of each loop iteration.
number
i
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);
Thanks man. That makes it easier. What is the incrementation process?
– TopChef
Jul 13 '10 at 23:39
range() returns a sequence (an abstraction that behaves like a list of numbers), and 'for number in...' is said to 'iterate over the sequence'. There's no exact analog to the sequence in the C version, but the relationship between the elements of the range sequence is where the 'incrementation' happens.
– Russell Borogove
Jul 14 '10 at 0:00
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
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:
i
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.
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
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.
range is the list of the numbers 1 to 100.
number then references each object in that list
number
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
That Java/C++ snippet will stop executing before the first
i++because of thereturn.– detly
Jul 13 '10 at 23:30