while 循环
Python 中 while 语句的一般形式:
while 判断条件(condition):执行语句(statements)……
#!/usr/bin/env python3n = 100sum = 0counter = 1while counter <= n:sum = sum + countercounter += 1print("1 到 %d 之和为: %d" % (n,sum))
无限循环
while 循环使用 else 语句
while <expr>:<statement(s)>else:<additional_statement(s)>
#!/usr/bin/python3count = 0while count < 5:print (count, " 小于 5")count = count + 1else:print (count, " 大于或等于 5")
for 语句:
格式:
for <variable> in <sequence>:<statements>else:<statements>
tmp =['a','b','c','d','e']for x in tmp:print(x)
range()函数
如果你需要遍历数字序列,可以使用内置range()函数。它会生成数列
for i in range(5):print(i)
pass 语句
pass 不做任何事情,一般用做占位语句,如下实例
class MyEmptyClass:... pass
