源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

使用Python的判断语句模拟三目运算

  • 时间:2020-01-14 12:39 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:使用Python的判断语句模拟三目运算
下面说的和三目运算有点相似,但又不一样,实在不知道该如何拟定标题,先就是这个标题吧,大家都知道python中没有三目运算,但是and/or有点类似三目运算: [b]and/or[/b] 单独使用表示逻辑关系与和或,也可以组和使用,用法如下 [b]and[/b] and前后如果某一个值为假(False, '', [], {}, None…)则返回第一个假值 如果所有值都为真则返回最后一个真值 [b]or[/b] 如果or任意一个值为真,则立刻返回这个值 如果所有值都为假,则or返回最后一个假值 [b]例子[/b]
result = 'test' and True # result = True
result = 'test' and 'ortest' # result = ortest
result = False and 'ortest' # result = False
result = '' and None # result = ''

result = '' or "Hall" # result = Hall
result = False or None # result = None
result = 'test' or 'nottest' # result = test

[b]使用单行if else 模拟三目运算[/b] result if True / False else fresult if为真时候结果为result,为假的时候结果为fresult
result = 'test' if True else 'not test' # result = 'test'
result = 'test' if False else 'not test' # result = 'not test'

  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部