Python's equivalent to PHP's null coalesce operator and shorthand ternary operator?

Viewed 4248

I was looking here and couldn't find anything: https://docs.python.org/3/library/operator.html

Two examples of PHP that come in handy a lot:

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

Are there equivalent operators in Python? Note:

a if condition else b

does not really replace the shorthand ternary operator, because condition and return value are specified in one element in PHP in the shorthand version.

2 Answers
Related