控制语句
判断
| if condition:
print()
elif condition:
print()
else:
print()
|
Example
| age = 20
if age > = 6:
print('teenager')
elif age >= 18:
print('adult')
else:
print('kid')
|
还可以简写
| if x:
print('True')
或
if x: print('True')
|
只要 x 是非零数值、非空字符串、非空list、非空tuple,也不是None,就判断为True,否则为False
循环
for
| for x in Iterable:
do sth
|
Example
| Li = [1, 2, 3, 4, 5, 6]
sum = 0
for x in Li:
sum += x
print(sum)
# 21
|
| sum = 0
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
sum = sum + x
print(sum)
#55
|
range()
| >>> range(5)
0, 1, 2, 3, 4
>>> list(range(5))
[0, 1, 2, 3, 4]
|
| # 从1加到100
sum = 0
for x in range(101):
sum = sum + x
print(sum)
# 5050
|
while
| sum = 0
n = 99
while n > 0:
sum = sum + n
n = n - 2
print(sum)
|
break
跳出整个循环,且不执行else子句
continue
跳出本次循环
else
else 子句会在循环不满足条件退出时执行
但如果 break 跳出循环时,不会执行else子句
pass
pass 语句什么也不做。当语法上需要一个语句,但程序需要什么动作也不做时,可以使用它。
| >>> while True:
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
>>> class MyEmptyClass:
... pass
>>> def initlog(*args):
... pass # Remember to implement this!
|