Python for loop?

前端 未结 6 1519
醉酒成梦
醉酒成梦 2021-01-24 10:04

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


        
相关标签:
6条回答
  • 2021-01-24 10:22

    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.

    0 讨论(0)
  • 2021-01-24 10:24

    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)

    0 讨论(0)
  • 2021-01-24 10:25

    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.

    0 讨论(0)
  • 2021-01-24 10:25

    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

    0 讨论(0)
  • 2021-01-24 10:34

    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.

    0 讨论(0)
  • 2021-01-24 10:46

    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.

    0 讨论(0)
提交回复
热议问题