python之九九乘法表

一、For循环

for i in range(1, 10):
    for j in range(1, i + 1):
        print('%d * %d = %2d  ' % (j, i, j * i), end=' ')
    print()

二、While循环

i = 1
while i <= 9:
    j = 1
    while j <= i:
        print("%d*%d=%-2d" % (j, i, j * i), end=" ")
        j += 1
    print()
    i += 1

三、列表推导式

print('\n'.join([' '.join(['%2d *%2d = %2d' % (i, j, i * j) for i in range(1, j + 1)]) for j in range(1, 10)]))

 

上一篇:hadoop基准測试


下一篇:B-比较两个日期