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 # full format output 99 multiplication table 2  for  in range (1,10):3for in      Range (1,10):4         Print("%d*%d=%2d" % (i,j,i*j), end="" )5     print("")

Output content:

2. Upper Left Triangle

Code:

1 #Upper Left triangle format output 99 multiplication table2  forIinchRange (1,10):3      forJinchRange (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 #Upper Right triangular format output 99 multiplication table2  forIinchRange (1,10):3     For K in range (1,i):4         Print (end= "")5      forJinchRange (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 layer of the loop statement more than two sentences (Code Red), 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 , which indicates that the end is the output of the content to the right of the equal sign, similar to the upper right and upper left.

4. Lower left Triangle

Code:

 1  #   2  for  i in  Range (1,10 3  for  J in  range (1,i+1 4   %d*%d=%2d  % (i,j,i*j), End="  Span style= "color: #800000;" > " "  5   ( " ) 

Output content:

5. Lower right triangle

Code:

1 #Lower right triangle output 99 multiplication table2  forIinchRange (1,10):3      forKinchRange (1,10-i):4         Print(end="       ")5      forJinchRange (1,i+1):6product=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.