Python for loop?

前端 未结 6 1594
醉酒成梦
醉酒成梦 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: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)

提交回复
热议问题