IF函数+While函数+For循环

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?

 

转载于:https://www.cnblogs.com/sjsmyy/p/8488499.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值