General Loop in Python Question

Hi, I have no questions about the assignment, but I do have a question about embedded loops in Python.

Specifically, when we use [i, j] iterators in the loops, does Python automatically know that [i] refers to the row of the dataset and [j] refers to the columns? The reason I ask is because [i, j] are not defined anywhere in the code until they are defined as iterators of the loop.

Also, we use m, n variables with X.shape. Do the variables m & n refer to different values in the in the dataset or is this a way of differentiating m & n when we’re iterating through [i, j] elements in the loops? Maybe Python is incapable of iterating through 2 loops within the same range (m).

Mathematically, I understand what we’re programming but the embedded loops get pretty complex pretty fast.

No big deal, just want to understand the code clearly. Thanks.

Hello @Rod_Bennett,

Each numpy array has a certain number of dimensions. For example, if we convert any tabular dataset into an array, that will be a 2 dimensional array, and let’s say we name that array as data_array. Another way of saying it is that data_array is an array of arrays.

When we do data_array[i], it gives us the i-th element of the data_array. Since that element is also an array, data_array[i] gives us the i-th array. Note that it is the i-th row of array.

So when we do data_array[i][j], it gives us the i-th element of data_array, which again is the i-th row, and then the j-th element of data_array[i]. Note that data_array[i][j] is equal to data_array[i, j].

My response is not fully answering your questions, but just as starting. For the rest, I would strongly suggest you to come up with the ideas yourself by a series of experiment.

You might start with a simple dataset like the following:

x = np.array([
    [10, 11, 12],
    [25, 26, 27],

Note that my above representation clearly show that it is an array of 2 arrays. It is a 2 rows times 3 columns dataset. Try to do x[1][2], x[2, 1], … (the action of adding square brackets next to an array to recall the content of that array is called “indexing”) and see if you can index the exact element you want from that table. As for the shape, just check it with x.shape and see what comes out. Lastly, don’t forget to change to some other x by like adding a few more rows or columns, and repeat all of the experiments and see if the outcomes always stick with your understanding.

For x.shape you might even need to hypothesize what it does unless you google for its exact use. However, I won’t be surprised if you can progressively get to the right answer after you try out x.shape with a few different shapes of x.

Most of the questions can actually be answered by experiments and it is literally a wonderful way to confirm understanding. If to a point you cannot find any exception in your understanding from experiments and you want to verify that ultimate understanding with some others, please feel free to post it as a statement here and hopefully someone can comment.


1 Like

No, it does not.


Thank you, it makes total sense.