29
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.

1
  • 6
    That Java/C++ snippet will stop executing before the first i++ because of the return. Commented Jul 13, 2010 at 23:30

6 Answers 6

27

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man. That makes it easier. What is the incrementation process?
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.
10

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

Comments

1

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.

Comments

1

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

Comments

0

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.

Comments

0

range is the list of the numbers 1 to 100.

number then references each object in that list

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.