mercredi 21 novembre 2018

Python defining a value before if, or if/else? [duplicate]

An if/else can be written as such:

if <condition>:
    variable = <value>
else:
    variable = <other value>

But the same can be achieved by:

variable = <other value>
if <condition>:
    variable = <value>

Is there a meaningful argument as to which is preferred? Or does it come down to personal preference?

Note: I know in Python you could also do:

variable = <value> if <condition> else <other value>

I'm only concerned about syntax.

Aucun commentaire:

Enregistrer un commentaire