Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ternary execution order [duplicate]

Tags:

python

In python, if I use a ternary operator:

x = a if <condition> else b

Is a executed even if condition is false? Or does condition evaluate first and then goes to either a or b depending on the result?

like image 950
jj172 Avatar asked Dec 07 '22 15:12

jj172


1 Answers

The condition is evaluated first, if it is False, a is not evaluated: documentation.

like image 200
ducminh Avatar answered Dec 10 '22 04:12

ducminh