How does looping "row" actually create the rows


#1

I feel like I’m missing something here. For exercise ~3-5 when we create our board we go from creating the five lists of five “O”'s all in-line:

[['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']]

to creating a more visually pleasing board

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

and I understand why and how we got there. What I’m not understanding is how using the following code based on Exercise 5

def print_board(board_in):
  for row in board:

is able to take the list at each index and place it on a new row. ‘’‘row’’’ is a normal variable, does Python just automatically add a new line at the end of each index/index?

That is

some_list = [ [1, 2, 3, 4] ["W", "X", "Y", "Z"] ["word1", "word2", "word3", "word4"] ]

print some_list

is just treated as

some_list = [ [1, 2, 3, 4] #print will automatically add a new line here
["W", "X", "Y", "Z"] #print will automatically add a new line here
["word1", "word2", "word3", "word4"] #print will automatically add a new line here
]

Did answer my own question?? :grin:


#2

python will automatically add a new line (\n) character at the end of each print statement. Which is why each print statement prints on its own line

but print some_list is only a single print statement, so the code will be all one line.


#3

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