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

开发 后端
Python逻辑需要我们进行相关的技术学校,下面我们就看看如何进行Python逻辑的使用。希望大家在之后的使用中有所收获。

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

举例:

  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逻辑的相关的介绍。

【编辑推荐】

  1. Python编程语言与Zpoe之间不解的情缘
  2. Python编程语言在网站开发中的妙用
  3. Python编程语言的发展历程介绍
  4. Python多线程具体运用的方法
  5. Python编程语言在未来的发展趋势
责任编辑:张浩 来源: IT168
相关推荐

2010-03-12 14:37:16

Python逻辑操作

2010-03-16 16:47:25

Python数组

2010-03-11 15:01:52

Python源码

2010-03-19 12:49:20

Python编程

2010-03-17 12:37:51

Python定时器

2010-03-16 18:59:47

Python模块

2010-03-17 12:53:43

Python Libr

2009-06-02 16:22:37

2010-03-19 15:16:11

Python代码

2010-03-12 10:21:48

Python函数

2010-03-23 14:54:27

Python目录文件

2012-05-11 11:47:55

存储虚拟化

2010-03-17 13:33:04

Python Libr

2013-04-25 10:03:30

应用交付解决方案负载均衡

2010-03-15 15:38:47

Python运行

2010-03-12 17:23:38

Python脚本

2023-02-03 17:47:28

2019-06-25 15:56:58

鸿蒙华为操作系统

2010-03-17 16:27:39

Python矩阵转置

2012-06-13 14:29:06

点赞
收藏

51CTO技术栈公众号