python的三目运算符和not in运算符使用示例


Posted in Python onMarch 03, 2014

三目运算符也就是三元运算符

一些语言(如Java)的三元表达式形如:

判定条件?为真时的结果:为假时的结果

result=x if x

Python的三元表达式有如下几种书写方法:

if __name__ == '__main__':
	a = ''
	b = 'True'
	c = 'False'
	
	#方法一:为真时的结果 if 判定条件 else 为假时的结果
	d = b if a else c
	print('方法一输出结果:' + d)
	
	#方法二:判定条件 and 为真时的结果 or 为假时的结果
	d = a and b or c
	print('方法二输出结果:' + d)
	
	#以上两种方法方法等同于if ... else ...
	if a:
		d = b
	else:
		d = c
	print('if语句的输出结果:' + d)

输出结果:

python的三目运算符和not in运算符使用示例

说明:
判断条件:a为空串,所以判断条件为假
当判断条件为真时的结果:d = b
当判断条件为假时的结果:d = c

x = [x for x in range(1,10)]
print(x)
y =[]

result = True if 12 not in x else False # this is the best way
print(result)
result = True if not 12 in x else False # this way just like as " (not 12) in x"
print(result)

print(x is y)
print(x is not y) # this is the best way
print(not x is y) # this way just like as " (not x ) is y" ,so upper is the best way

result = 2 if 1 < 2 else 5 if 4 > 5 else 6 # just as 1 > 2 ? 2 : 4 > 5 ? 5 : 6
print(result)

python中的not具体使用及意思

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)`) 。

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

>>> 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

更多内容可以参考这篇文章:https://3water.com/article/93165.htm

Python 相关文章推荐
Python中字符串的格式化方法小结
May 03 Python
Java Web开发过程中登陆模块的验证码的实现方式总结
May 25 Python
Python使用自带的ConfigParser模块读写ini配置文件
Jun 26 Python
Swift中的协议(protocol)学习教程
Jul 08 Python
Python Image模块基本图像处理操作小结
Apr 13 Python
python机器学习库scikit-learn:SVR的基本应用
Jun 26 Python
使用Python操作ArangoDB的方法步骤
Feb 02 Python
最新PyCharm 2020.2.3永久激活码(亲测有效)
Nov 26 Python
python中四舍五入的正确打开方式
Jan 18 Python
Python趣味实战之手把手教你实现举牌小人生成器
Jun 07 Python
Python中字符串对象语法分享
Feb 24 Python
Python+Selenium实现读取网易邮箱验证码
Mar 13 Python
python使用cookielib库示例分享
Mar 03 #Python
python使用cookie库操保存cookie详解
Mar 03 #Python
简单的通用表达式求10乘阶示例
Mar 03 #Python
python显示天气预报
Mar 02 #Python
pyqt4教程之实现半透明的天气预报界面示例
Mar 02 #Python
windows下python模拟鼠标点击和键盘输示例
Feb 28 #Python
python抓取网页中的图片示例
Feb 28 #Python
You might like
Discuz7.2版的faq.php SQL注入漏洞分析
2014/08/06 PHP
PHP的RSA加密解密方法以及开发接口使用
2018/02/11 PHP
PhpStorm 如何优雅的调试Hyperf的方法步骤
2019/11/24 PHP
goto语法在PHP中的使用教程
2020/09/17 PHP
Prototype 学习 工具函数学习($w,$F方法)
2009/07/12 Javascript
浅谈 jQuery 事件源码定位问题
2014/06/18 Javascript
js防止DIV布局滚动时闪动的解决方法
2014/10/30 Javascript
javascript对象的创建和访问
2016/03/08 Javascript
修复jQuery tablesorter无法正确排序的bug(加千分位数字后)
2016/03/30 Javascript
jQuery插件FusionWidgets实现的Bulb图效果示例【附demo源码下载】
2017/03/23 jQuery
基于BootStrap的文本编辑器组件Summernote
2017/10/27 Javascript
200行代码实现blockchain 区块链实例详解
2018/03/14 Javascript
在JS循环中使用async/await的方法
2018/10/12 Javascript
VUE v-model表单数据双向绑定完整示例
2019/01/21 Javascript
微信小程序常用的3种提示弹窗实现详解
2019/09/19 Javascript
[15:58]DOTA2国际邀请赛采访专栏:Tongfu.Sansheng&KingJ,DK.rOtk
2013/08/08 DOTA
pip matplotlib报错equired packages can not be built解决
2018/01/06 Python
python截取两个单词之间的内容方法
2018/12/25 Python
Python argparse模块应用实例解析
2019/11/15 Python
Python爬虫爬取杭州24时温度并展示操作示例
2020/03/27 Python
零基础小白多久能学会python
2020/06/22 Python
python 实现波浪滤镜特效
2020/12/02 Python
利用CSS3的flexbox实现水平垂直居中与三列等高布局
2016/09/12 HTML / CSS
原装进口全世界:天猫国际
2016/08/03 全球购物
DAWGS鞋官方网站:鞋,凉鞋,靴子
2016/10/04 全球购物
Sahajan美国:阿育吠陀护肤品牌
2021/01/09 全球购物
机电一体化专业应届生求职信
2013/11/27 职场文书
生日宴会主持词
2014/03/20 职场文书
家长寄语大全
2014/04/02 职场文书
辅导员评语
2014/05/04 职场文书
租房协议书范文
2014/08/20 职场文书
继承公证书格式
2015/01/26 职场文书
会计简历自我评价
2015/03/10 职场文书
企业计划生育责任书
2015/05/09 职场文书
入门学习Go的基本语法
2021/07/07 Golang
nginx作grpc的反向代理踩坑总结
2021/07/07 Servers