Topic: Output 9*9 multiplication formula table.
Procedure analysis: Consideration of rows and columns, a total of 9 rows and 9 columns, i control row, j control column.
Summary: Understanding the inner and outer execution logic of for loop nesting
Got it!!! The first level for loop takes an element out, and then enters the second level of loop. The second level of loop traverses all elements before jumping out of this level of cycle.
Enter the outer loop and execute all the cycles by analogy.
Therefore, the outer cycle is higher than the inner cycle. After the inner cycle has been executed, it jumps out of the inner cycle before continuing the outer cycle, and then enters the inner cycle.
For instance:
The outer cycle has nine numbers, and the inner cycle has nine numbers. When the outer cycle is executed one number, it enters the inner cycle, and the inner cycle executes nine. The outer layer executes 9 times, and the inner layer executes 9 x 9 = 81 times.
Method 1: end="" Print without line change
for i in range(1, 10): for j in range(1, 10): print ("%d*%d=%d" % (i,j, i*j),"\t",end="") if i == j: print() break
Test:
PS C:\Users\zms\work> python .\test.py 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 PS C:\Users\zms\work>
Method 2:
for i in range(1,10): l=[] for j in range(1,i+1): l.append(str(j)+"*"+str(i)+"="+str(i*j)) print(" ".join(l))
Method 3:
for i in range(1, 10): for j in range(1, i+1): print('%dx%d=%d' %(i,j,i*j),' ',end="") if i==j: print() break
Output results:
PS C:\Users\zms\work> python .\test.py 1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 PS C:\Users\zms\work>
Method 4: The print() function is executed outside the inner loop, so that the logic of the loop is to wrap lines at the end of each inner loop.
If you put the print() function in the inner loop, the logic is:
#Form 1: for i in range(1, 10): print() for j in range(1, i+1): print('%dx%d=%d' %(i,j,i*j),' ',end="") #Result: PS C:\Users\zms\work> python .\test.py 1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 PS C:\Users\zms\work> #Form 2 for i in range(1, 10): for j in range(1, i+1): print('%dx%d=%d' %(i,j,i*j),' ',end="") print() #Result: PS C:\Users\zms\work> python .\test.py 1x1=1 2x1=2 2x2=4 3x1=3 3x2=6 3x3=9 4x1=4 4x2=8 4x3=12 4x4=16 5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 PS C:\Users\zms\work>
As you can see, the difference between the two forms is that because the print() function is first executed in Form 1, the output of Form 1 has one blank line in the first line.
Method 5: Do it in one line
[[print(f"{i} x {j} = {i * j}", end=' ') if (i + 1) > j else print('\n') for j in range(1, i + 2)] for i in range(1, 10)]