python中的not具体使用及意思(关键词:Python/not)
来源:互联网 发布:淘宝买的鞋子味道很大 编辑:程序博客网 时间:2024/06/03 18:32
name=”
while not name:
name=raw_input(u'请输入姓名:')
print name
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
x = 1
not x
False
x = [1]
not x
False
x = 0
not x
True
x = [0] # You don’t want to fall in this one.
not x
False
在python中 None, False, 空字符串”“, 0, 空列表[], 空字典{}, 空元组()都相当于False ,即:
[python] view plaincopy
not None == not False == not ” == not 0 == not [] == not {} == not ()
因此在使用列表的时候,如果你想区分x==[]和x==None两种情况的话, 此时if not x:
将会出现问题:
[python] view plaincopy
x = []
y = Nonex is None
False
y is None
Truenot x
True
not y
Truenot x is None
True
not y is None
False也许你是想判断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的区别
问题 链接
if foo is None: pass
if foo == None: pass
如果比较相同的对象实例,is总是返回True 而 == 最终取决于 “eq()”
class foo(object):
def eq(self, other):
return Truef = foo()
f == None
True
f is None
Falselist1 = [1, 2, 3]
list2 = [1, 2, 3]
list1==list2
True
list1 is list2
False
另外
(ob1 is ob2) 等价于 (id(ob1) == id(ob2))
#
补充,2013.10.09
转自http://zhidao.baidu.com/question/514056244.html
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
参考文献:
1. python中的not具体使用及意思。
- python中的not具体使用及意思(关键词:Python/not)
- python中的is ,is not
- python提取文件中的关键词及部分上下文内容
- python第三天 随机数,not 使用
- Python变量命名及关键词
- Python Is Not Java
- Python Is Not Java
- python is not None
- python not含义
- python not 关键字
- 使用python中的numpy模块遇到could not convert string to float
- python 关键词
- Python开发环境安装及Project interpreter not specified问题解决
- Python开发环境安装及Project interpreter not specified问题解决
- python中not,and,or的优先级问题及用法
- python eric 中的错误:The file could not be opened.
- python 中的is / is not / == / !=
- Python中的str is not callable问题分析
- HTM(皮质学习)Youtube课程学习笔记二(episode 4)
- Node-red学习第6篇--Map a property between different numeric ranges
- 使用Hribernate内连接时hql语句必须加上“fetch”关键字
- linux sed编辑器
- Kali Linux渗透测试(苑房弘)Sqlmap
- python中的not具体使用及意思(关键词:Python/not)
- cmake源码编译安装jsoncpp
- Android开发之网络操作(下)
- Kali Linux渗透测试(苑房弘)w3af
- [log]logstash添加字段.geoip展示
- 9.11~9.16训练----重要!动规dp
- linux下mysql的root密码忘记解决方
- tensorflow实战1:lstm实现mnist分类
- wind and sun