What is the method of printing ninety-nine multiplication in different Python formats
This article mainly introduces "what is the method of printing ninety-nine multiplication in different Python formats". In daily operation, I believe that many people have doubts about the method of printing ninety-nine multiplication in different Python formats. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "what is the method of printing ninety-nine multiplication in different Python formats?" Next, please follow the editor to study!
The ninety-nine multiplication tables of output rectangle, upper left triangle, upper right triangle, lower left triangle and lower right triangle are realized by Python. The Python version I use is Python 3.2.2.
1. Complete rectangular format
Code:
# full format output 99 multiplication table 2 for i in range (1j10): 3 for j in range (1mag10): 4 print ("% d*%d=%2d"% (iLING j), end= ") 5 print (")
Output:
two。 Upper left triangle
Code:
# output in the upper left triangle format 99 multiplication table 2 for i in range (1mag10): 3 for j in range (iMag10): 4 print ("% d*%d=%2d"% (iGramage j), end= ") 5 print (")
Output:
Note: the multiplication formula is output by line, compared with the complete format, the inner loop range is iS9, when the I of the outer loop increases gradually, the number of formulas output per line will become less and less, print (") indicates a line break, if you do not output this sentence, the output multiplication table is out of order.
3. Upper right triangle
Code:
# output ninety-nine multiplication table for i in range (1p10): for k in range (1PowerI): print (end= "") for j in range (iPower10) in upper right triangle format: print ("% d*%d=%2d"% (end= ") print ("))
Output:
Note: compared with the upper left triangle, the inner loop statement has two more sentences (code red). Because each formula occupies a position of 7 bytes, it outputs the corresponding number of spaces beyond the previous space. In Python, the print ("") statement cannot be written directly to indicate the output space. The end keyword must be added to indicate that the end ends with the equal sign.
The content output of the edge is similar to the difference between the upper right and the upper left.
4. Lower left triangle
Code:
# output in lower left triangle format 99 multiplication Table 2 for i in range (1 for i in range 10): 3 for j in range (1 print 1): 4 print ("% d*%d=%2d"% (iLJ jJournal iMagj), end= ") 5 print (")
Output:
5. Lower right triangle
Code:
# output ninety-nine multiplication table for i in range (1j10) in lower right triangle format: for k in range (1mem10ripi): print (end= "") for j in range (1memire1): product=i*j print ("% d*%d=%2d"% (igraine jpen1 product), end= ") print (")
Output:
At this point, the study of "what is the method of printing ninety-nine multiplication in different Python formats" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!