Python for loop?

2020-04-27 16:38发布

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.

标签: python
6条回答
地球回转人心会变
2楼-- · 2020-04-27 16:45

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-04-27 16:48

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

查看更多
冷血范
4楼-- · 2020-04-27 16:52

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.

查看更多
对你真心纯属浪费
5楼-- · 2020-04-27 16:52

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.

查看更多
\"骚年 ilove
6楼-- · 2020-04-27 16:53

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)

查看更多
够拽才男人
7楼-- · 2020-04-27 16:58

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.

查看更多
登录 后发表回答