我在这里找不到任何东西:https://docs.python.org/3/library/operator.html
PHP的两个例子很方便派上用场:
echo $count ?: 10; //prints $count if $count is not empty string, null, false or 0, otherwise prints 10.
echo $a ?? $b ?? 7; //prints $a if $a is defined and not null, otherwise will print $b, otherwise 7
Run Code Online (Sandbox Code Playgroud)
Python中是否有等效的运算符?注意:
a if condition else b
Run Code Online (Sandbox Code Playgroud)
并不真正替换速记三元运算符,因为条件和返回值是在速记版本的PHP中的一个元素中指定的.
小智 6
该or运营商返回第一个真y值.
a = 0
b = None
c = 'yep'
print(a or 'nope')
print(b or 'nope')
print(c or 'nope')
print(b or c or 'nope')
> nope
> nope
> yep
> yep
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
909 次 |
| 最近记录: |