A certainl part of an if() structure never gets executed.

Jussi Piitulainen jpiitula at ling.helsinki.fi
Fri Jun 14 14:17:13 EDT 2013


Nick the Gr33k writes:

>  >>> (a or b or c)
> 'abcd'
> 
> This for me, should evaluate to True but instead it has been
> evaluated to the first variable's value, which is a truthy value of
> course since its not an empty string, but shouldn't it return True
> instead?

In your own programs, write bool(a or b or c) instead. And instead of
writing (k in (a or b or c)), write ((k in a) or (k in b) or (k in c))
-- you can use fewer parentheses if you like, but only if you are
comfortable with it. (Here, I use the outer parentheses to separate
Python from English. I might not use them in code.)

Usually such expressions occur as conditions in conditional statements
or conditional expressions. There, the bool() makes no observable
difference.

> Returning True is the same thing as returning a variable's truthy
> value?

I think that's a good approximation. Strictly speaking, True is a
particular value in Python, but I think that at the moment you need to
understand that what is important is how the value is interpreted as a
condition in a conditional statement (if condition:, elif: condition),
a conditional expression (x if condition else y), a while loop (while
condition:).

           >>> while "ijkl": print("it doesn't matter")



More information about the Python-list mailing list