嵌入式 pyhton中的判断语句

在Python中比较语句和其它的一样都是用if来做判断的,只是语法上后面会带上冒号,如ifa>b:,相当于if(a>b){}.

同样你可以判断函数的返回值为True或者False来做判断

[python] viewplain copy print ?
  1. __author__="Alex"  
  2. __date__ ="$2011-2-17 10:39:27$"  
  3. if __name__ == "__main__":  
  4.     print "Hello";  
  5.  
  6.  
  7.  
  8. True statements  
  9. if == b: print('c is equal as b')  
  10. if >= b: print('...')  
  11. if >= b: print('a is greater than b')  
  12. if <= a: print('c is less than a.')  
  13. if != a: print('c is not equal as a')  
  14. False statements  
  15. if == a: print('...')# will show nothing on console  
  16. if != b: print('...')# will show nothing on console  
  17. def inside():  
  18.     return True  
  19. if inside():  
  20.     print ("Inside")  
  21. else:  
  22.     print("OutSide")  
 

Console OutPut:

[c-sharp] viewplain copy print ?
  1. Hello  
  2. is equal as  
  3. ...  
  4. is greater than  
  5. is less than a.  
  6. is not equal as  
  7. Inside  
 

多重比较:

当然你可能会用到 且或这些操作,在Python中是使用and/or 来做到的,看代码吧,从代码中学习语言才是王道:

  1. __author__="Alex"  
  2. __date__ ="$2011-2-17 10:39:27$"  
  3. if __name__ == "__main__":  
  4.     print "Hello";  
  5. if and 3: print('You are right is greater than and is greater than too') will print, True and True becomes True  
  6. if and 3: print('...') will not print, True and False becomes False  
  7. if and not 3: print('You are right is greater than and is less than ') will print, True and not False becomes True  
  8. if or 3: print('You are right...,When is grater than is ture, the condition is ture') will print, True or True becomes True  
  9. if (4 and 3) and not or 7): print("(True and True) and not False or False becomes True and not False")#Complex Multiple Conditions  

Console Output:

  1. Hello  
  2. You are right is greater than and is greater than too  
  3. You are right is greater than and is less than   
  4. You are right...,When is grater than is ture, the condition is ture  
  5. (True and True) and not False or False becomes True and not False  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值