1

I am having trouble understanding a Python for loop. For example, here is some code I've made while I'm learning:

board = []
for i in range (0,5):
    board.append(["O"] * 5)

Don't worry about what the code does, I just don't understand what the "i" variable means.

3
  • 6
    I find it helps to strew print all over the place when I'm trying to understand something. Here, I'd add print i inside the loop. (Or print(i) if you're using Python 3.) Commented Apr 2, 2013 at 12:25
  • 1
    docs.python.org/2/tutorial/controlflow.html#for-statements Commented Apr 2, 2013 at 12:27
  • Don't forget to accept an answer! This helps everyone else show that the question has been completely answered :). Commented Apr 5, 2013 at 20:22

6 Answers 6

4

Think of it as substitution.

range(0,5) is [0,1,2,3,4]. The for-loop goes through each element in the list, naming the element i.

for i in range(0,5):
    # Starts with 0
    print i # prints 0
    # now goes back, goes through next element in list: 1.

Prints 0,1,2,3,4.

In your example, i is a placeholder. It is used simply just to loop something x amount of times (in this case, five as the length of range(0,5) is 5)

Also, have fun learning python at Codecademy (I recognise the task :p)

Sign up to request clarification or add additional context in comments.

8 Comments

Haha :P The interpreter is actually quite messed up, because half of the time I'm writing totally correct code and I get errors (I know this because I've run the code in my IDLE window)
Yea, it's a bit buggy! But it's not too bad to start learning python. :p.
It's pretty good, although I'm having a bit of trouble because of the interpreter..... and it doesn't always explain things the best. But I'm learning better than I did with my book, at least for now.
There's also a forum to check out on each topic: try checking there if you have any trouble :). Alas, I actually switched to LPTHW (learn python the hard way) for understanding classes, it's still difficult for me :(. Anyways, enough rambling from me, enjoy learning python :D!
The interpreter is the same whether you run code from a file or type it in. You are probably just getting your indentation incorrect, at a guess.
|
4

It's an iterator, you can see it as a bucket that stores the result of each iteration; the thing that adds confusion is the fact that it's simply unused in your script, this is another script with a "more involved" use of iterators.

fruits = ['banana', 'apple', 'strawberry', 'coconut', 'cherry']
for yup in fruits:
  print(yup)

as you can see you can name it as you want, it's the syntax that makes that word an iterator.

Comments

3

It's an unused variable. Python syntax requires a variable in that position, but you don't do anything with it since you simply want to repeat an action 5 times.

Some people prefer the convention of naming an unused variable like this _:

for _ in range(5)

but this name can interfere with gettext.

Comments

1

In short, i refers to the current element in the list.

Your list is defined as: 0, 1, 2, 3, 4 and 5. Therefore i will iterate this list and assign itself to the next item, i is 0, next iteration i will be 1, next iteration i will be 2 etc.

Directly from python.org:

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended)

words = ['cat', 'window', 'defenestrate']
for w in words:
 print w, len(w)

Results in:

  • cat 3
  • window 6
  • defenestrate 12

http://docs.python.org/2/tutorial/controlflow.html

Comments

1

The for loop iterates over the given list of objects which is [0, 1, 2, 3, 4] obtained from range(0,5) and in every iteration, you need a variable to get the iterated value. That is the use i here. You can replace it with any variable to get the value.

for n in range(0, 5):
    print n    #prints 0, then 1,  then 2, then 3,then 4 in each iteration

Another example:

for n in ('a', 'b', 'c'):
    print n    #prints a, then b,  then c in each iteration

But in the code you have given, the variable i is not used. It is being used. just to iterate over the list of objects.

Comments

1

In c/java the for loop wiil be:

for(int i=0;i<=10;i++)
{ 
  //for-loop-body
}

here for every iteration i will be incrementing +1 value till i reaches 10, after that it comes out of loop. In same way,in python the for loop looks like:

for i in range(0,10):
  //for-loop-body

here i performs same operation and i is just a variable to increment a value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.