1. The outer for loop uses the built-in function range to load each number in the range from 1 to 9 into the custom variable I in turn. At this point, variable I is cyclically assigned for 9 times
2. The inner for loop will load each number in the range of 1~ variable I into variable J in turn, and then variable j will be assigned a value of I times. At this time, the outer for loop will circulate once and the inner for loop will circulate for I times
3. The range value node of the inner layer for loop should be the outer variable I plus 1, so that the inner variable j can take the value of I
4. At the end of the program, print() is equivalent to enter, which is executed every time the external loop is completed to beautify the execution result
Step 1: Write the script
1. vim mtable.py
1. #!/usr/bin/env python3
1. for i in range(1.10) : # [0.1.2]
1. for j in range(1, i+1): # i->0: [0], i->1: [0.1], i->2: [0.1.2]
1. print('%sX%s=%s' % (j, i, i*j), end=' ')
1. print()
1. [root@localhost day03]# vim mtable.py
1. #!/usr/bin/env python3
1. i=1
1. while i<=9:
1. j=1
1. while j<=i:
1. print("%d*%d=%d" % (j,i,j*i),end="")
1. j+=1
1. print("")
1. i+=1
Copy the code
Step 2: Test script execution
- python3 mtable.py
- 1X1=1
- 1X2=2 2X2=4
- 1X3=3 2X3=6 3X3=9
- 1X4=4 2X4=8 3X4=12 4X4=16
- 1X5=5 2X5=10 3X5=15 4X5=20 5X5=25
- 1X6=6 2X6=12 3X6=18 4X6=24 5X6=30 6X6=36
- 1X7=7 2X7=14 3X7=21 4X7=28 5X7=35 6X7=42 7X7=49
- 1X8=8 2X8=16 3X8=24 4X8=32 5X8=40 6X8=48 7X8=56 8X8=64
- 1X9=9 2X9=18 3X9=27 4X9=36 5X9=45 6X9=54 7X9=63 8X9=72 9X9=81