700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python打印九九乘法表五种方法

python打印九九乘法表五种方法

时间:2019-08-05 22:55:37

相关推荐

python打印九九乘法表五种方法

目录

1.九九乘法表for-for

运算结果:

2.九九乘法表while-while

运算结果:

3.九九乘法表while-for

运算结果:

4.九九乘法表for-while

运算结果:

5.定义一个变量

5.1变量为列表

运行结果:

5.2定义一个元组

运行结果:

1.九九乘法表for-for

for i in range(1, 10): #range函数是左开右闭for j in range(1, i+1):print(f"{i}*{j}={i * j}\t", end=" ")print()

运算结果:

'''D:\AXXZX\Python\python\python.exe D:\python_code\if_while_for.py 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=124*4=165*1=5 5*2=105*3=155*4=205*5=256*1=6 6*2=126*3=186*4=246*5=306*6=367*1=7 7*2=147*3=217*4=287*5=357*6=427*7=498*1=8 8*2=168*3=248*4=328*5=408*6=488*7=568*8=649*1=9 9*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81Process finished with exit code 0'''

2.九九乘法表while-while

i = 1while i < 10:j = 1while j < i + 1 : #j的大小的有i来控制的,还可以将语句换成 while(j <= i):print(f"{i}*{j}={i * j}",end="\t")j += 1print()i += 1

运算结果:

'''D:\AXXZX\Python\python\python.exe D:\python_code\if_while_for.py 1*1=12*1=22*2=43*1=33*2=63*3=94*1=44*2=84*3=124*4=165*1=55*2=105*3=155*4=205*5=256*1=66*2=126*3=186*4=246*5=306*6=367*1=77*2=147*3=217*4=287*5=357*6=427*7=498*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=649*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81Process finished with exit code 0'''

3.九九乘法表while-for

i = 1while i < 10:for j in range(1,i+1): #j的大小的有i来控制的print(f"{i}*{j}={i*j}", end="\t")print()i += 1

运算结果:

'''D:\AXXZX\Python\python\python.exe D:\python_code\if_while_for.py 1*1=12*1=22*2=43*1=33*2=63*3=94*1=44*2=84*3=124*4=165*1=55*2=105*3=155*4=205*5=256*1=66*2=126*3=186*4=246*5=306*6=367*1=77*2=147*3=217*4=287*5=357*6=427*7=498*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=649*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81Process finished with exit code 0'''

4.九九乘法表for-while

for i in range(1, 10):j = 1while j < i+1:print(f"{i}*{j}={i*j}", end="\t")j += 1print()i += 1

运算结果:

'''D:\AXXZX\Python\python\python.exe D:\python_code\if_while_for.py 1*1=12*1=22*2=43*1=33*2=63*3=94*1=44*2=84*3=124*4=165*1=55*2=105*3=155*4=205*5=256*1=66*2=126*3=186*4=246*5=306*6=367*1=77*2=147*3=217*4=287*5=357*6=427*7=498*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=649*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81Process finished with exit code 0'''

5.定义一个变量

5.1变量为列表

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]for i in a:for j in range(1, i+1):print(f"{j}*{i}={j*i}", end='\t')j += 1print()

运行结果:

"""D:\AXXZX\Python\python\python.exe D:\python_code\program_run.py 1*1=11*2=22*2=41*3=32*3=63*3=91*4=42*4=83*4=124*4=161*5=52*5=103*5=154*5=205*5=251*6=62*6=123*6=184*6=245*6=306*6=361*7=72*7=143*7=214*7=285*7=356*7=427*7=491*8=82*8=163*8=244*8=325*8=406*8=487*8=568*8=641*9=92*9=183*9=274*9=365*9=456*9=547*9=638*9=729*9=81Process finished with exit code 0"""

5.2定义一个元组

b = (1, 2, 3, 4, 5, 6, 7, 8, 9)for i in b:j = 1while j <= i:print("%d*%d=%d" % (j, i, j*i), end='\t')j += 1print()

运行结果:

"""D:\AXXZX\Python\python\python.exe D:\python_code\program_run.py 1*1=11*2=22*2=41*3=32*3=63*3=91*4=42*4=83*4=124*4=161*5=52*5=103*5=154*5=205*5=251*6=62*6=123*6=184*6=245*6=306*6=361*7=72*7=143*7=214*7=285*7=356*7=427*7=491*8=82*8=163*8=244*8=325*8=406*8=487*8=568*8=641*9=92*9=183*9=274*9=365*9=456*9=547*9=638*9=729*9=81Process finished with exit code 0"""

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。