Python implements 99 multiplication tables in different formats

Source: Internet
Author: User

Through Python, the output rectangle full format, the upper left triangle, the upper right triangle, the left lower triangle and the lower right triangle in five formats 99 multiplication table.

1. Rectangular full format

Code:

1 #完整格式输出九九乘法表2 for I in Range (1,10): 3     for J in Range (1,10): 4         print ("%d*%d=%2d"% (i,j,i*j), end= "") 5     print (" ")

Output content:

2. Upper Left Triangle

Code:

1 #左上三角格式输出九九乘法表2 for I in Range (1,10): 3     for J in Range (i,10): 4         print ("%d*%d=%2d"% (i,j,i*j), end= "") 5     Print ("") 6         

Output content:

Note: Multiplication calculation by row output, compared with the full format, the inner loop range is i~9, when the outer loop of I gradually increment, the number of the output of each line will be less and fewer, print ("") for the line, do not output this sentence output multiplication table type of confusion.

3. Upper Right Triangle

Code:

1 #右上三角格式输出九九乘法表2 for I in Range (1,10): 3 for     K in range (1,i): 4         print (end= "       ") 5 for     J in Range (i,10): 6
   
    print ("%d*%d=%2d"% (i,j,i*j), end= "") 7     print ("")
   

Output content:

Note: Compared to the upper left triangle, the inner loop statement is more than two sentences (Code Red place), because each equation occupies a position of 7 bytes, so the extra space in front of the output of the corresponding number of spaces, in Python can not directly write print ("") statement to indicate the output space, you must add the end keyword, Represents the output ending with the right side of the equal sign, similar to the difference between the upper right and upper left of the following.

4. Lower left Triangle

Code:

1 #左下三角格式输出九九乘法表2 for I in Range (1,10): 3     for J in Range (1,i+1): 4         print ("%d*%d=%2d"% (i,j,i*j), end= "") 5     pri NT ("")

Output content:

5. Lower right triangle

Code:

1 #右下三角格式输出九九乘法表2 for I in Range (1,10): 3 for     K in range (1,10-i): 4         print (end= "       ") 5 for     J in Range (1,i+1): 6         product=i*j7         Print ("%d*%d=%2d"% (i,j,product), end= "") 8     print ("")

Output content:

Python implements 99 multiplication tables in different formats

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.