''' Purpose: another take on nested loops ''' rows = 4 cols = 3 # We are printing 3 rows and 4 columns. # ---------- SIMPLE SINGLE FOR LOOP -------------------- for r in range( 0, rows ) : print( r ) # This for loop will simply go from range (0,4) values 0,1,2,3 and print r one by one as it changes value with # each iteration of the loop. print() print() # -------------- NESTED FOR LOOP ------------------------ for r in range( 0, rows ) : print( r ) for c in range( 0, cols ) : print( " ", r, c ) # This is a nested for loop! For each r value, it will do do everything indented into that for loop. So what does that mean? # For each r we have to print r and finish the c loop inside before moving onto the next r value! # So this means that for each r value, we are going to print the r value # and then print everything else in that c loop before it changes to the next r value. # Ask me how this works later in office hours if you're still confused! print() print() # -------------- NESTED FOR LOOP PT. 2 ----------------------- for r in range( 0, rows ) : print( r ) for c in range( 0, cols ) : sum = r + c print( " ", r, c, sum ) # This is also a nested for loop which will go through each row and print out r and then do the c loop before moving back up to the next r value. # So for this for loop, it basically looks like the last one but it prints out the sum of r and c in each line for each r as well. print() print() # ---------------- NESTED FOR LOOP PT. 3 ------------------------ for r in range( 0, rows ) : for c in range( 0, cols ) : sum = r + c print( sum, end=" " ) print() # This final loop just prints out all the sums for r + c for the r we are currently on on the same line and moves onto the next r value. print() print()