import numpy def squares(matrix, square_size): paint = False for row in xrange(len(matrix)): # Only in the borders of the square, we change the switch to paint if row % square_size == 0: paint = not paint # We need to store the initial status of the row, because it # defines the behaviour of the pixels in the entire row. If # we don't store it, the status of the next row depends on the # status of the last pixel in this rows (and the patterns becomes # really weird!) initial_status = paint for col in xrange(len(matrix[row])): # Only in the borders of the square, we change the switch to paint if col % square_size == 0: paint = not paint if paint: matrix[row][col] = 0 paint = initial_status matrix = numpy.ones((10, 10)) squares(matrix, input("Square size: ")) print matrix