条件语句
- if 表达式 :执行语句
如果if语句判断是True,则执行后面的语句
age=20
if age>20:
print '>20'
print '>20'
print '<=20'
执行结果:
>20
>20
<=20
- if 表达式 :执行语句 else : 执行语句
如果if判断是False,不要执行if的内容,去把else执行了
age=21
if age>20:
print '>20'
else:
print '<=20'
执行结果:
>20
- if 表达式 :执行语句 elif 表达式 : 执行语句 else : 执行语句
多次判断
age=21
if age>20:
print '>20'
elif age==20:
print '=20'
else:
print '<=20'
执行结果:
>20
循环语句
- for...in
names = ['tom', 'Bob']
for name in names:
print(name)
输出结果:
tom
Bob
- while循环
sum = 0
n = 99
while n > 0:
sum = sum + n
n = n - 2
print(sum)