IF函数:
单分支IF条件语法:
if 判断条件: if 判断条件:
print 执行结果 等效于=====> print 执行结果
else: if not 判断条件:
print 执行结果 print 执行结果
多分支IF条件语法
if 判断条件1:
print 执行结果1
elif 判断条件2:
print 执行结果2
。。。。。。。。
elif 判断条件n
print 执行结果n
else :
执行结果
注:这一系列条件判断会从上到下依次判断,如果某个判断为 True,执行完对应的代码块,后面的条件判断就直接忽略,不再执行了。
For循环语法
For循环一般哟过来遍历数组,例如List数组或者tuple数组的每隔数值
Eg:
L = [75, 92, 59, 68]
sum = 0.0
for name in L:
sum=sum+name
print sum / 4
For循环中的Break和Continue的用法
Break的用法:跳出本次循环,后面循环都不执行
for i in range (1,6):
print
print 'i=',i,
print 'Hello,what is ',
if i==3:
break
print 'the weather today?'
执行结果:
i= 1 Hello,what is the weather today?
i= 2 Hello,what is the weather today?
i= 3 Hello,what is
Continue的用法:跳出本次循环,后面依旧执行
for i in range (1,6):
print
print 'i=',i,
print 'Hello,how',
if i==3:
continue
print 'are you today?'
执行结果:
i= 1 Hello,how are you today?
i= 2 Hello,how are you today?
i= 3 Hello,how
i= 4 Hello,how are you today?
i= 5 Hello,how are you today?
i= 6 Hello,how are you today?