[Tutor] nested "for" loops

Peter Jakubowicz beyondthezero@earthlink.net
Fri May 2 22:48:02 2003


Hi,

I've been slogging along learning Python for a while now. Nested "for" 
loops confuse me (I have trouble trying to run through them in my head). 
For example, does the following code generate (albeit redundantly) all 
Pythagorean triples up to 20: i.e., all integers less than or equal to 20 
for which i * i + j * j == k * k

TIA,
Peter



for i in range(1, 21):
     for j in range(1, 21):
         for k in range(1, 21):
             if (i * i) + (j * j) == (k * k):
                 print "Pythagorean triple: %d, %d, %d" % (i, j, k)