神剑山庄资源网 Design By www.hcban.com
三目运算符也就是三元运算符
一些语言(如Java)的三元表达式形如:
判定条件"htmlcode">
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)
输出结果:
说明:
判断条件: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 "htmlcode">name='' while not name: name=raw_input(u'请输入姓名:') print namepython中的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://www.jb51.net/article/93165.htm
标签:
三目运算符
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无python的三目运算符和not in运算符使用示例的评论...
更新日志
2024年10月04日
2024年10月04日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】