How to Run Three Loops in Python
The Python language supports "for" loops, which iterate through a list of values from zero to a specified number. You can embed loops and perform multiple loops in your Python code. You must specify a different variable for each loop, so the variables separate each loop and they do not overlap and change values unintentionally.
Instructions
-
-
1
Open your Python editor and open the project you want to edit. Open the page you want to edit to load the code in the editor.
-
2
Add the first loop to the code. The following code adds the first, opening loop:
for x in xrange(0,10):
In this example, the "x" variable iterates from zero to 10.
-
-
3
Add the second and third loop. The following code adds the next two loops and nests them within the initial "for" loop with the "x" variable:
for y in xrange(0,10):
for z in xrange(0, 10)
print '%d * %d = %d' % (x,y,z, x*y*z)In this example, using the "x" main loop, the nested loops also iterate and multiply all variables together. After the multiplication, the results print to the screen.
-
1