斐波那契数列
a,b = 0,1
while b<10 :
print(b)
a,b = b,a+b
1
1
2
3
5
8
end 关键字 用于将后果输入到同一行,或者在输入的开端增加不同的字符
c, d = 0, 1
while d < 1000:
print(d, end=',')
c, d = d, c+d
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
while、else 语句
count = 0
while count < 5:
print (count, "小于 5")
count = count + 1
else:
print (count, "大于或等于 5")
0 小于 5
1 小于 5
2 小于 5
3 小于 5
4 小于 5
5 大于或等于 5
for 语句
languages = ["C", "Java", "Php", "Python"]
for x in languages:
print (x)
C
Java
Php
Python
range() 函数遍历数字序列,联合 len() 函数
for i in range(10):
print(i)
print("-------------------")
for i in range(0, 10, 2) :
print(i)
print("-------------------")
a = ['Google', 'Baidu', 'Runoob', 'Taobao', 'QQ']
for i in range(len(a)):
print(i, a[i])
0
1
2
3
4
5
6
7
8
9-------------------
0
2
4
6
8-------------------
0 Google
1 Baidu
2 Runoob
3 Taobao
4 QQ
pass 语句
pass 是空语句,是为了放弃程序结构的完整性
pass 不做任何事件,个别用做占位语句
参考:Python3 循环语句 | 菜鸟教程