Grids are not formally a data structure in Python. Nevertheless, you will see that the use of grids is everywhere when addressing computer graphics and design. Pixels, after all, are organized as a grid structure. We will think of grids as a spatial organization of information, one that will have some specific properties based on the type of grid we construct.

Let's start by creating a simple grid:

#define the resolution of a grid
reso_x = 10
reso_y = 10

#define the separation between cells
cell_size_x = 5
cell_size_y = 5

#We'll do a nested loop - one loop for our iteration in X and another for Y
for i in range(0, reso_x):
        
    for j in range(0, reso_y):

        #Define the position of a cell by the i and j iteration
        pos_x = (i * cell_size_x) 
        pos_y = (j * cell_size_y)

The example above introduces some variables:

The resolution of the grid in X and Y, also known as the COLUMNS and ROWS of the grid.

Each entity in the grid we will call a CELL , and we can establish a separation between cells by defining a CELL SIZE .

The example below calculates the size of a cell to be adaptive to the size of a canvas:

# Canvas dimensions
image_size_x = 1200
image_size_y = 600

#define the resolution of a grid
reso_x = 10
reso_y = 10

#Define the size of a Cell by dividing the size of the screen by the resolution of the grid
cell_size_x = image_size_x / reso_x
cell_size_y = image_size_y / reso_y

#We'll do a nested loop - one loop for our iteration in X and another for Y
for i in range(0, reso_x):
        
    for j in range(0, reso_y):

        #Define the position of a cell by the i and j iteration provided in the loop, multiplied by the size of the cell
        pos_x = (i * cell_size_x) 
        pos_y = (j * cell_size_y)