Explain my loop, pls


So. My code just print board which need to complete the quest. But i can`t understand why my loop in print_board() made 5 times? Im a bit stuck with this loops after C++.

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

print print_board(board)


board is a list which contains 5 elements. so looping over board will give you each element. If you decide to print the element, you get 5 lines printed


So Python saw 5 elements and automatically know that loop iterates only up to 5?


your for loop:

for row in board:

row is your iterator. its gets assigned each item in the list (in order), so yes, python determines the length of the list, and will loop over it

Python takes care of all this for you, quit a difference from C++


Yes, i see that loops are different. Ok, think i can understand this. Thanks. :3


Python is interpenetrated by C (Cphyton), so someone wrote C code which enable us to make this simply loops, under the hood, there is some clever C code


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.