Python—三目运算
- 2019 年 10 月 3 日
- 笔记
Python ??? if ??????????????????????? if ?????????????????? if ??????????
(True_statements) if (expression) else (False_statements)
???????????????? expression ???????????? True??????? True_statements ???????????? False??????? False_statements ???
?????
>>> isinput = True if input('input: ') else False input: 996ICU >>> isinput True >>> isinput = True if input('input: ') else False input: >>> isinput False
?????????
?? = ?1 or ?2
????? = ?1 if ?1 else ?2
??????
>>> content = input('input: ') or 'nothing' input: 996ICU >>> content '996ICU' >>> content = input('input: ') or 'nothing' input: >>> content 'nothing'
???print???
a = 996 b = 666 str_ = '996' if a < b else '666' print(str_) print('996' if True else 'icu') print('996') if False else print('icu')
666 996 icu
Python????????? True_statements ? False_statements ????????
Python ?????????????
1.???????????????????????????????????????
>>> str_ = print('996'), 'True' if 5 > 0 else 'False', 'icu' 996 >>> str_ (None, 'True', 'icu')
2.????????????????????????????????????
>>> str_ = 'icu'; st = 'True' if 5 > 10 else print('ICU') ICU >>> str_ 'icu' >>> print(st) None
??????
print('aaa') if True else (print('bbb') if False else print('ccc')) print('aaa') if False else (print('bbb') if False else print('ccc'))
aaa ccc