and/or
单独使用表示逻辑关系与和或,也可以组和使用,用法如下
and
and前后如果某一个值为假(false, '', [], {}, none…)则返回第一个假值 如果所有值都为真则返回最后一个真值
or
如果or任意一个值为真,则立刻返回这个值 如果所有值都为假,则or返回最后一个假值
例子
result = 'test' and true # result = trueresult = 'test' and 'ortest' # result = ortestresult = false and 'ortest' # result = falseresult = '' and none # result = ''result = '' or hall # result = hallresult = false or none # result = noneresult = 'test' or 'nottest' # result = test
使用单行if else 模拟三目运算
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'
