if-else statement

Scott David Daniels Scott.Daniels at Acm.Org
Sat Jan 10 21:12:07 CET 2009

Duncan Booth wrote:
> Gandalf <goldnery at gmail.com> wrote:
>> other languages ... [have an] if-else operator like...
>>      myVar = checking  == 1? 'string': 'other string'
> See http://docs.python.org/reference/expressions.html#boolean-operations
> ...
> The expression x if C else y first evaluates C (not x); if C is true, x is 
> evaluated and its value is returned; otherwise, y is evaluated and its 
> value is returned.

While we _do_ have the form <expr> if <test> else <expr>, it is
generally agreed that in most cases your code will be more clear
with explicit tests and assignments.  Your particular example
is better

      if checking:
          my_var = 'string'
          my_var = 'other string'

remember, vertical space only kills trees if printed.

--Scott David Daniels  (who still prints this too frequently).
Scott.Daniels at Acm.Org

More information about the Python-list mailing list