Python for different formats printing 99 multiplication table

Source: Internet
Author: User

In the recent study of Python, learning resources are online video tutorials, novice tutorials, and official Python document tutorial. Although the basic syntax of Python has been understood, there is no real sense of outputting the code that you write. Code small white, previously only learned C, the logic of the code is always unclear, plus character a bit impatient, always halfway, so the programming level is slag. Now, although we are just beginning to do the testing work, but do not want their work confined to functional testing, but also want to have access to performance testing and even white box testing of the programming has a certain requirements of the work. Since the nature of the project currently being tested belongs to Web testing, start with the current work and begin learning Python and the database. The use of Blog Park often organize their own learning content, to consolidate and strengthen the purpose. 99 The multiplication table should be the most elementary programming, but in the implementation process still encountered a lot of problems, C and Python in many places is not the same, sometimes with C thinking to consider the Python programming problem, but ignore the Python programming should pay attention to the place.

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. The Python version I used is Python 3.2.2.

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     Prin T ("")

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 for different formats printing 99 multiplication table

Related Article

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.