Question about print_board() [5/19]


Hi everyone, I have a doubt, when we use for, the iteration does on background the i++? What I don't understand is how the console know that has to print -board[0]- then jump one line and continue till the 5th line.
Here is my code that actually works, but I don't get the concept.

board = []

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

print_board (board)

So, it will print:

['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']

Q1: How does python know to print one line and jump to the other?
Q2: I thought also that I had to write that it has to print index [0], then jump and continue.

Thanks in advance.


Hello, @zeby11!
If you noticed board is a list that has other lists on it.
If you do:

print type(board)

You'll get that the type of the board is list.
Okay but you probably now this by know so how does Python know that you need to skip a line?
In fact, you don't have "lines", you have same sized lists inside of another list.
While iterating through the board list, you wrote:

def print_board(board):
    for i in board:
         print board

This is just like iterating through a list, where each position of it has a list. Cool, huh?
Okay so here's the picture of it:

If you noticed, inside of the list board you have other five lists and Python will continue to print the values stored at each index until the "big list", the one who stores them all, has no other lists to iterate.
I believe it works this way:

Program: "Oh, we have a list here? That's nice! Let's execute things!"
Program: "Hmmm, this list is different from the others. Other than simply having simple values inside of it, it has lists... nice! Guess I'll have to print each of them and continue to check if the board list still have members that still weren't printed. Let's keep working!"

The idea is: the for loop will first check if there are elements to be print on the list board. If there are, the execution goes and prints it but since it's another list, it will check if there are elements to be printed and print them.


bigList = [[1,2,3,4,5], 6]
print bigList[0] # Gets [1,2,3,4,5]
print bigList[1] #Gets 6

This will go on until the execution reached the end of this same list (the list who's inside the big list that we call as "board").
It leaves the first list, board[0], and checks if board still have elements to be printed and it goes on.

So if you noticed, Python does the "index++" for you, so you don't really have to worry about it.
Now here's an explanation in a more "technical" way about for loops:

for iterating_var in sequence:

If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.

Here are some really good tutorials about it: Python - For Loop and ForLoop
Well, if after all of this you still didn't understand, let me know and I'll find another way to explain it!


Oh man, it's brilliant the way you explained me theFor Loop. Thank you very much!!


Oh, I'm glad you liked it!
Glad to help :slightly_smiling: