python是否有语法糖“ condition?c:d”

Jim*_*ang -2 python

Java,c,c ++都有这样的语法糖:

int a = condition ? c : d// if condition is true , a = c, else a = d
Run Code Online (Sandbox Code Playgroud)

python有类似的语法糖吗?

Tim*_*o D 5

是的:)

a = c if condition else d
Run Code Online (Sandbox Code Playgroud)

这是Python 2.5中引入的