Python知识学习04——循环

while循环

比如:打印1-10

1
2
3
4
a = 1
while a <= 10:
print(a)
a += 1

这里补充一个Python使用Print()函数输出,实现不换行的方式:

1
2
3
4
a = 1
while a <= 5:
print(a, end = "")
a += 1

小练习:使用Python的while循环打印99乘法表

1
2
3
4
5
6
7
8
9
a = 1
b = 1
while a <= 9:
while b <= a:
print("{} * {} = {}".format(b, a, a * b), end = "\t")
b += 1
b = 1
a += 1
print("")

for循环初见

1
2
3
name = "Tom"
for temp in name:
print(temp)