How to Nest Loop Statements in Python - dummies

By John Paul Mueller

In some cases, you can use either a for loop or a while loop to achieve the same effect in Python. The manners work differently, but the effect is the same. In this example, you create a multiplication table generator by nesting a while loop within a for loop. Because you want the output to look nice, you use a little formatting as well.

  1. Open a Python File window.

    You see an editor in which you can type the example code.

  2. Type the following code into the window — pressing Enter after each line:

    X = 1
    Y = 1
    print ('{:>4}'.format(' '), end= ' ')
    for X in range(1, 11):
     print('{:>4}'.format(X), end=' ')
    for X in range(1,11):
     print('{:>4}'.format(X), end=' ')
     while Y <= 10:
      print('{:>4}'.format(X * Y), end=' ')

    This example begins by creating two variables, X and Y, to hold the row and column value of the table. X is the row variable and Y is the column variable.

    To make the table readable, this example must create a heading at the top and another along the side. When users see a 1 at the top and a 1 at the side, and follow these values to where they intersect in the table, they can see the value of the two numbers when multiplied.

    The first print() statement adds a space. All the formatting statement says is to create a space 4 characters wide and place a space within it. The {:>4} part of the code determines the size of the column. The format(‘ ‘) function determines what appears in that space. The end attribute of the print() statement changes the ending character from a carriage return to a simple space.

    The first for loop displays the numbers 1 through 10 at the top of the table. The range() function creates the sequence of numbers for you. When using the range() function, you specify the starting value, which is 1 in this case, and one more than the ending value, which is 11 in this case.

    At this point, the cursor is sitting at the end of the heading row. To move it to the next line, the code issues a print() call with no other information.

    Even though the next bit of code looks quite complex, you can figure it out if you look at it a line at a time. The multiplication table shows the values from 1 * 1 to 10 * 10, so you need ten rows and ten columns to display the information. The for statement tells Python to create ten rows.

    The first print() call displays the row heading value. Of course, you have to format this information, and the code uses a space of four characters that end with a space, rather than a carriage return, in order to continue printing information in that row.

    The while loop comes next. This loop prints the columns in an individual row. The column values are the multiplied values of X * Y. Again, the output is formatted to take up four spaces. The while loop ends when Y is updated to the next value using Y+=1.

    Now you’re back into the for loop. The print() statement ends the current row. In addition, Y must be reset to 1 so that it’s ready for the beginning of the next row, which begins with 1.

  3. Choose Run→Run Module.

    You see this multiplication table.