Python逻辑操作中的三大应用方案

Python逻辑在运作中有不少的问题需要解决。下面我们就详细的看看如何进行实际的操作。在实际的使用中有三种:and、or、not。分别对应与、或、非。希望大家有所收获。

10年积累的成都网站设计、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站策划后付款的网站建设流程,更有永州免费网站建设让你可以放心的选择与我们合作。

举例:

 
 
 
  1. #coding:utf-8
  2. test1 = 12
  3. test2 = 0
  4. print (test1 > test2) and (test1 > 14) #result = False
  5. print (test1 < test2) or (test1 > -1) #result = True
  6. print (not test1) #result = False
  7. print (not test2) #result = True

严格的说,逻辑操作符的操作数应该为布尔表达式。但Python逻辑操作对此处理的比较灵活。即使操作数是数字,解释器也把他们当成“表达式”。非0的数字的布尔值为1,0的布尔值为0.

举例:

 
 
 
  1. #coding:utf-8
  2. test1 = 12
  3. test2 = 0
  4. print (test1 and test2) #result = 0
  5. print (test1 or test2) #result = 12
  6. print (not test1) #result = Flase
  7. print (not test2) #reslut = True

在Python逻辑操作中,空字符串为假,非空字符串为真。非零的数为真。

数字和字符串之间、字符串之间的逻辑操作规律是:

对于and操作符:只要左边的表达式为真,整个表达式返回的值是右边表达式的值,否则,返回左边表达式的值对于or操作符:只要两边的表达式为真,整个表达式的结果是左边表达式的值。

如果是一真一假,返回真值表达式的值,如果两个都是假,比如空值和0,返回的是右边的值。(空值或0)举例:

 
 
 
  1. #coding:utf-8 
  2. test1 = 12
  3. test2 = 0 
  4. test3 = '' 
  5. test4 = "First" 
  6. print test1 and test3 #result = '' 
  7. print test3 and test1 #result = '' 
  8. print test1 and test4 #result = "First" 
  9. print test4 and test1 #result = 12
  10. print test1 or test2 #result = 12
  11. print test1 or test3 #result = 1212 print test3 or test4 
    #result = "First"
  12. print test2 or test4 #result = "First"
  13. print test1 or test4 #result = 12
  14. print test4 or test1 #result = "First"
  15. print test2 or test3 #result = ''
  16. print test3 or test2 #result = 0

以上就是对Python逻辑的相关的介绍。

网页标题:Python逻辑操作中的三大应用方案
URL分享:http://www.hantingmc.com/qtweb/news36/31386.html

成都网站建设公司_创新互联,为您提供响应式网站定制网站商城网站ChatGPT面包屑导航动态网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联