# Printing Pretty

#1

Hello everyone,

Please correct me if i m wrong, as i m trying to understand every detail in python:
first:
board = []
for i in range(5):
board.append([‘O’] * 5)
print (board)

this will print:

[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]

if i m correct for every element from 0 until 5 (excluded ), when i =0 we will multiply O*0 (5 times) this will lead us to first print [[‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
then i = 0 and 1 we print : [[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
hen i = 0 and 1 and 2 we print : [[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]] [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]

etc…

my question is since in board we have

board=
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]
[[‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’, ‘0’, ‘0’]]

Why when we add this code:

board = []
for i in range(5):
board.append([‘O’] * 5)

def print_board(board_in):
for row in board_in:

``````print " ".join(row)
``````

print_board(board)

we obtain this result:

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 not this result:

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, 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
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

#2

the board is 5*5, so printing each iteration of the loop while we construct the board will first print one row, then two rows, three rows and so forth. Given we print again and again each time we add a row. If you would do:

``````for i in range(5):
board.append([‘O’] * 5)
print (board)
``````

you also see a 5*5 board.

the print_board function only prints the final result

#3

thank you very much… it is very clear for me now…

#4

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