原则:缩进原则,相同缩进视为代码块
4个空格,不要使用Tab,更不要混合Tab和空格
if
>>> age = 20
>>> if age >= 18:
... print 'your age is', age
... print 'adult'
if-else
if age >= 18:
print 'adult'
else:
print 'teenager'
if-elif-else
if age >= 18:
print 'adult'
elif age >= 6:
print 'teenager'
elif age >= 3:
print 'kid'
else:
print 'baby'
for
L = ['Adam', 'Lisa', 'Bart']
for name in L:
print name
while
x = 0
while x < 10:
print x
x = x + 1
break退出循环
sum = 0
x = 1
while True:
sum = sum + x
x = x + 1
if x > 100:
break
print sum
continue跳过本次循环,继续下一次循环