开发者学堂课程【Python 入门 2020年版:打印九九乘法表】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/639/detail/10272
打印九九乘法表
打印九九乘法表
打印 5 个星星:
i = 0
while i < 5:
i += 1
print(‘*’, end=” “)
print()
运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python.exe C:/Users/chris/Desktop/Python 基础/Day04-流程
* * * * *
Process finished with exit code 0
End 表示打印一个星星换行。
在外面嵌套一个 j:
j = 0
while j < 5:
j += 1
i = 0
while i < 5:
i += 1
print(‘*’, end=” “)
print()
运行结果:C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python基础/Day04-流程
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Process finished with exit code 0
打印三角形:
j = 0
while j < 5:
j += 1
i = 0
while i < j:
i += 1
print(‘*’, end=” “)
print()
运行结果:C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python基础/Day04-流程
*
* *
* * *
* * * *
* * * * *
Process finished with exit code 0
打印 9 行:
j = 0
while j < 9:
j += 1
i = 0
while i < j:
i += 1
print(‘*’, end=” “)
print()
运行结果:C:\Users \chris\AppDatalLocal\Programs \Python\Python37\python. exe C:/Users/ chris/Desktop/Python基础/Day04-流程
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Process finished with exit code 0
在 while j < 9: 和 while i < j: 处打断点,当 j=0 时 j < 9 ,满足条件,j=1,i = 0,0<1 满足 i=1。接着打印一个星星不换行。
运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python exe . "C:\Program Files\JetBrains\PyCharm Communi
pydev debugger: process 6200 is connecting
Connected to pydev debugger (build 193.6015.41)
*
走完下面后,内循环循环体 I += 1 pirnt(‘*’,end=””) 已走完,走完后就会回来判断条件,这是 i = 1, j = 1,1<1 不满足条件,就不再进入,就会走 print(),
运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python exe . "C:\Program Files\JetBrains\PyCharm Communi
pydev debugger: process 6200 is connecting
Connected to pydev debugger (build 193.6015.41)
*
对于上面的星星 i 和 j 都等于1。等到换行走完了,走完外循环的循环体 j += 1
i = 0
while i < j:
i += 1
print(‘*’, end=” “)
循环走完就会回来判断条件,j 是 1, 1<9 满足条件,进入 j=2,i 又重新等于 0,0<2 满足条件,i=1,打印星星不换行,
运行结果如下:C:\Users\chris\AppData\Local\Programs\Python\Python37\python exe . "C:\Program Files\JetBrains\PyCharm Communi
pydev debugger: process 6200 is connecting
Connected to pydev debugger (build 193.6015.41)
*
*
对于第二个星星 i=1,j=2 。等
while i < j:
i += 1
print(‘*’, end=” “)
走完后,回来判断条件,i 是 1,j 是 2 满足条件,再进入,这是 i=2,
运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python exe . "C:\Program Files\JetBrains\PyCharm Communi
pydev debugger: process 6200 is connecting
Connected to pydev debugger (build 193.6015.41)
*
* *
打印第二行第二个星星是 i 和 j 都等于 2。2<2 不满足条件,就直接走到 print(),运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python exe . "C:\Program Files\JetBrains\PyCharm Communi
pydev debugger: process 6200 is connecting
Connected to pydev debugger (build 193.6015.41)
*
* *
走完循环体又重新走,2<9 满足条件,进入 j=3,i=0。0<3 满足条件,i=1,
运行结果:
打印第二行第二个星星是 i 和 j 都等于 2。2<2 不满足条件,就直接走到 print() ,运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python exe . "C:\Program Files\JetBrains\PyCharm Communi
pydev debugger: process 6200 is connecting
Connected to pydev debugger (build 193.6015.41)
*
* *
*
第三行的星星 j=3,i=1,1<3 满足条件,进入 i=2 ,运行结果:C:\Users\chris\AppData\Local\Programs\Python\Python37\python exe . "C:\Program Files\JetBrains\PyCharm Communi
pydev debugger: process 6200 is connecting
Connected to pydev debugger (build 193.6015.41)
*
* *
* *
第三行第二个星星 i 是 2j 是 3。打印 i 和 j,
j = 0
while j < 9:
j += 1
i = 0
while i < j:
i += 1
print(i,j,end=”“)
#多打几个空格
print()
运行结果:C: \Users\chris\AppData\Local\Programs \Python\Python37\python.exe c:/Users/chris/Desktop/Python基砒 /Day04-流程11
1222
132333
14243444
1525354555
162636465666
17273747576777
1828384858687888
192939495969798999
Process finished with exit code 0
现已打印数字,
j = 0
while j < 9:
j += 1
i = 0
while i < j:
i += 1
print(i,’*’,j,end=”)
#多打几个空格
print()
运行结果:C: \Users\chris\AppData\Local\Programs \Python\Python37\python.exe c:/Users/chris/Desktop/Python基砒 /Day04-流程1*1
1*2 2*2
1*3 2*3 3*3
1*4 2*4 3*4 4*4
1*5 2*5 3*5 4*5 5*5
1*6 2*6 3*6 4*6 5*6 6*6
1*7 2*7 3*7 4*7 5*7 6*7 7*7
1*8 2*8 3*8 4*8 5*8 6*8 7*8 8*8
1*9 2*9 3*9 4*9 5*9 6*9 7*9 8*9 9*9
Process finished with exit code 0
再加个=
j = 0
while j < 9:
j += 1
i = 0
while i < j:
i += 1
print(i,’*’,j,’=’, end=” “)
#多打几个空格
print()
运行结果:C: \Users\chris\AppData\Local\Programs \Python\Python37\python.exe c:/Users/chris/Desktop/Python基砒 /Day04-流程
1*1=
1*2= 2*2=
1*3= 2*3= 3*3=
1*4= 2*4= 3*4= 4*4=
1*5= 2*5= 3*5= 4*5= 5*5=
1*6= 2*6= 3*6= 4*6= 5*6= 6*6=
1*7= 2*7= 3*7= 4*7= 5*7= 6*7= 7*7=
1*8 = 2*8= 3*8= 4*8= 5*8= 6*8= 7*8= 8*8=
1*9= 2*9= 3*9= 4*9= 5*9= 6*9= 7*9= 8*9= 9*9=
Process finished with exit code 0
再加相乘的结果
j = 0
while j < 9:
j += 1
i = 0
while i < j:
i += 1
print(i,’*’,j,’=’ ,(i*j)end=” “)
#多打几个空格
print()
运行结果:
C: \Users\chris\AppData\Local\Programs \Python\Python37\python.exe c:/Users/chris/Desktop/Python基砒 /Day04-流程
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
Process finished with exit code 0
j = 0
while j < 9:
j += 1
i = 0
while i < j:
i += 1
print(i,’*’,j,’=’ ,(i*j),sep=” “end=” “)
print()
sep 可以消除空格
运行结果:C: \Users\chris\AppData\Local\Programs \Python\Python37\python.exe c:/Users/chris/Desktop/Python基砒 /Day04-流程
Process finished with exit code 0
没对其,因为 end 有 4 个空格,将 4 个空格换成t键:
j = 0
while j < 9:
j += 1
i = 0
while i < j:
i += 1
print(i,’*’,j,’=’ ,(i*j),sep=” “end=”\t“)
print()
运行结果:C: \Users\chris\AppData\Local\Programs \Python\Python37\python.exe c:/Users/chris/Desktop/Python基砒 /Day04-流程
Process finished with exit code 0