python中的not具体表示是什么:

在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法:

(1) not与逻辑判断句if连用,代表not后面的表达式为False的时候,执行冒号后面的语句。比如:

a = False

if not a:  (这里因为a是False,所以not a就是True)

print "hello"

这里就能够输出结果hello

(2) 判断元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,这句话的意思是如果a不在列表b中,那么就执行冒号后面的语句,比如:

a = 5

b = [1, 2, 3]

if a not in b:

print "hello"

这里也能够输出结果hello

not x 意思相当于 if x is false, then True, else False

代码中经常会有变量是否为None的判断,有三种主要的写法:

 第一种是`if x is None`;

第二种是 `if not x:`;

第三种是`if not x is None`(这句这样理解更清晰`if not (x is None)`) 。

如果你觉得这样写没啥区别,那么你可就要小心了,这里面有一个坑。先来看一下代码:
[python] view plaincopy

 
  1. >>> x = 1
  2. >>> not x
  3. False
  4. >>> x = [1]
  5. >>> not x
  6. False
  7. >>> x = 0
  8. >>> not x
  9. True
  10. >>> x = [0]         # You don't want to fall in this one.
  11. >>> not x
  12. False
在python中 None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False ,即:
[python] view plaincopy

 
  1. not None == not False == not '' == not 0 == not [] == not {} == not ()
因此在使用列表的时候,如果你想区分x==[]和x==None两种情况的话, 此时`if not x:`将会出现问题:
[python] view plaincopy

 
  1. >>> x = []
  2. >>> y = None
  3. >>>
  4. >>> x is None
  5. False
  6. >>> y is None
  7. True
  8. >>>
  9. >>>
  10. >>> not x
  11. True
  12. >>> not y
  13. True
  14. >>>
  15. >>>
  16. >>> not x is None
  17. >>> True
  18. >>> not y is None
  19. False
  20. >>>
也许你是想判断x是否为None,但是却把`x==[]`的情况也判断进来了,此种情况下将无法区分。
对于习惯于使用if not x这种写法的pythoner,必须清楚x等于None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。 
而对于`if x is not None`和`if not x is None`写法,很明显前者更清晰,而后者有可能使读者误解为`if (not x) is None`,因此推荐前者,同时这也是谷歌推荐的风格 结论:
`if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法。
使用if not x这种写法的前提是:必须清楚x等于None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行。 ================================================================
不过这并不适用于变量是函数的情况,以下转载自:https://github.com/wklken/stackoverflow-py-top-qa/blob/master/contents/qa-control-flow.md

foo is None 和 foo == None的区别

问题 链接

  1.  
    if foo is None: pass
  2.  
    if foo == None: pass

如果比较相同的对象实例,is总是返回True 而 == 最终取决于 "eq()"

  1.  
    >>> class foo(object):
  2.  
    def __eq__(self, other):
  3.  
    return True
  4.  
     
  5.  
    >>> f = foo()
  6.  
    >>> f == None
  7.  
    True
  8.  
    >>> f is None
  9.  
    False
  10.  
     
  11.  
    >>> list1 = [1, 2, 3]
  12.  
    >>> list2 = [1, 2, 3]
  13.  
    >>> list1==list2
  14.  
    True
  15.  
    >>> list1 is list2
  16.  
    False

另外

(ob1 is ob2) 等价于 (id(ob1) == id(ob2))

python中not的用法的更多相关文章

  1. python 中del 的用法

    python中的del用法比较特殊,新手学习往往产生误解,弄清del的用法,可以帮助深入理解python的内存方面的问题. python的del不同于C的free和C++的delete. 由于pyth ...

  2. python中argparse模块用法实例详解

    python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...

  3. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  4. python中MySQLdb模块用法实例

    篇文章主要介绍了python中MySQLdb模块用法,以实例形式详细讲述了MySQLdb模块针对MySQL数据库的各种常见操作方法,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了python中 ...

  5. python中hashlib模块用法示例

    python中hashlib模块用法示例 我们以前介绍过一篇Python加密的文章:Python 加密的实例详解.今天我们看看python中hashlib模块用法示例,具体如下. hashlib ha ...

  6. Python Deque 模块使用详解,python中yield的用法详解

    Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...

  7. Python中super的用法【转载】

    Python中super的用法[转载] 转载dxk_093812 最后发布于2019-02-17 20:12:18 阅读数 1143  收藏 展开 转载自 Python面向对象中super用法与MRO ...

  8. Python中With的用法

    在看Dive Into Python中有关描述文件读写那章节的时候,看到了有关with的用法,查阅下相关资料,记录下来,以备后用. 官方的reference上有关with statement是这样说的 ...

  9. Python中AND-OR的用法

    学习Python中的lambda函数的时候,才发现原来Python中的AND和OR还可以有一些别的用法.Python中的布尔逻辑计算的结果并非返回布尔值,而是返回它们相互之间的某一个.文章的部分例子来 ...

  10. python中的有趣用法

    本文给除了python中几个有趣的用法,可以给我们不一样的启发 1: Python中模拟使用C++ 中的   cout << import sys  class ostream: def  ...

随机推荐

  1. img标签显示本地文件

    html: <img src="__IMG__/male.png" id="imgfpic1" style="height: 100%; wid ...

  2. Spring分布式事务

    [如何实现XA式.非XA式Spring分布式事务] [http://www.importnew.com/15812.html] 在JavaWorld大会上,来自SpringSource的David S ...

  3. sql print

    这个因为你使用了varchar+int ,但是print只支持一种类型的输出,你要么通过转换函数将@no转换成字符类型,要么去掉@name.print '李勇' + convert(varchar, ...

  4. tableau 常识积累

    没怎么在业务系统中使用过,所以需要好好积累.看起来很简单的东西都需要慢慢来用.下了一份它的官方文档10.3版本的.公司网络限制,不能去它官网学习.只有下班时间了. 先说一个,有时候度量值它和HANA类 ...

  5. Android Design TextinputLayout

    使用该布局 需要在build.gradle中的dependencies块中添加两个依赖来向下兼容 dependencies { compile fileTree(dir: 'libs', includ ...

  6. 是因为Session只能让服务器在一次连续的会话中记住你,而Cookie是记住浏览器一段时间

    Cookie的作用 因为http协议先天不足是无记忆性. 还有一个区别是:Session是服务器端保存会话状态的机制. 而Cookie则是浏览器端保存会话的机制. Cookie 的应用

  7. BEC listen and translation exercise 6

    能听懂自己的录音,说明发音还行,可惜听不懂... Another problem is that the members of the Biramichi fishing cooperative ar ...

  8. shell 实现闰年的判断

    #!/bin/shecho "please input the year"read year let "n1=$year % 4"let "n2=$y ...

  9. Jtable实现

    package database; import java.util.Vector; import javax.swing.table.AbstractTableModel; public class ...

  10. bzoj 3771 Triple——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3771 把方案作为系数.值作为指数,两项相乘就是系数相乘.指数相加,符合意义. 考虑去重.先自 ...