Python3里的循环嵌套结构打印99乘法表

1.双重for循环结构

for x in range(起始值,终止值,步长):
	执行程序1
	for y in range(起始值,终止值,步长):
		执行程序2

外循环跑一次,内循环跑全程
内循环可以配合break和continue来使用
break:跳出当前循环,进入外循环
continue:跳出当次循环,继续内循环

for x in range(1,11):
	print(x)
	for x in range(1,6):
		print(x)
		if(x==3):
		break

用双重for打印99乘法表的三种方式

for x in range(1,10):
	for y in range(1,10):
		print(y,"*",x,"=",x*y,end="\t")
		if(y == x):
			print()
			break
print("multiplication Table")
for i in range(1,10):
    for j in range(1,i+1):
        print(j,"*",i,"=",i*j,end="\t")
    print()
for x in range(1,10):
	for y in range(1,10):
		if(y > x):
			continue
		print(y,"*",x,"=",x*y,end="\t")
	print()

获得的结果均为

1 * 1 = 1	
1 * 2 = 2	2 * 2 = 4	
1 * 3 = 3	2 * 3 = 6	3 * 3 = 9	
1 * 4 = 4	2 * 4 = 8	3 * 4 = 12	4 * 4 = 16	
1 * 5 = 5	2 * 5 = 10	3 * 5 = 15	4 * 5 = 20	5 * 5 = 25	
1 * 6 = 6	2 * 6 = 12	3 * 6 = 18	4 * 6 = 24	5 * 6 = 30	6 * 6 = 36	
1 * 7 = 7	2 * 7 = 14	3 * 7 = 21	4 * 7 = 28	5 * 7 = 35	6 * 7 = 42	7 * 7 = 49	
1 * 8 = 8	2 * 8 = 16	3 * 8 = 24	4 * 8 = 32	5 * 8 = 40	6 * 8 = 48	7 * 8 = 56	8 * 8 = 64	
1 * 9 = 9	2 * 9 = 18	3 * 9 = 27	4 * 9 = 36	5 * 9 = 45	6 * 9 = 54	7 * 9 = 63	8 * 9 = 72	9 * 9 = 81

2.while循环嵌套if判断语句

i=1#列
j=1#行
while(j<10):
    print(i,"*",j,"=",i*j,end="\t")
    i=i+1
    if(i>j):
        print()
        i=1
        j=j+1

你可能感兴趣的:(python,笔记)