Home > Backend Development > Python Tutorial > How to print 99 multiplication table in python

How to print 99 multiplication table in python

PHPz
Release: 2020-09-27 11:34:49
Original
11541 people have browsed it

How to print the 99 multiplication table in Python: 1. Use [for-for]; 2. Use [while-while]; 3. Use [while-for]; 4. Use [for-while]; 5. Define a variable a, the code is [for i in a:j=1].

How to print 99 multiplication table in python

Related learning recommendations: python tutorial

How to print the 99 multiplication table in python:

The first way: use for-for

# 九九乘法表
for i in range(1, 10):
    for j in range(1, i+1):
        print('{}x{}={}\t'.format(j, i, i*j), end='')
    print()
Copy after login

How to print 99 multiplication table in python

The 2nd way: Use while-while

# 九九乘法表
i = 1
while i <= 9:
    j = 1
    while(j <= i):    # j的大小是由i来控制的
        print(&#39;%d*%d=%-3d&#39; % (i, j, i*j), end=&#39;\t&#39;)
        j += 1
    print(&#39;&#39;)
    i += 1
Copy after login

How to print 99 multiplication table in python

##The 3rd way: Use while-for

i = 1
while(i <=9):
    for j in range (1,i+1): #range()函数左闭右开
        print(&#39;%d*%d=%-3d&#39;%(i,j,i*j),end=&#39;&#39;)
    i += 1
    print()
Copy after login

How to print 99 multiplication table in python

The fourth way: use for-while

for i in range(1,10):
    j = 0
    while j < i:
        j += 1
        print("%d*%d=%-3d"%(i,j,i*j),end=&#39;&#39;)
    print( )
Copy after login

How to print 99 multiplication table in python

The 5th way: Define a variable a

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in a:
    j = 1
    while j <= i:
        print(&#39;%d*%d=%-3d&#39;%(i,j,i*j),end=&#39;\t&#39;)
        # %-3d 是控制输出结果占据3位,且从左面开始对齐
        j += 1
    print( )
Copy after login

The execution result is as follows:

How to print 99 multiplication table in python

The 6th way: Use 1 line The execution result of statement

print(&#39;\n&#39;.join([&#39; &#39;.join(["%2s x%2s = %2s" % (j, i, i*j) for j in range(1, i+1)]) for i in range(1, 10)]))
Copy after login
is as follows:

How to print 99 multiplication table in python

The above is the detailed content of How to print 99 multiplication table in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template