Nothing is transformed; Python Boolean logic operators instead of short circuits.
See the documentation for boolean statements :
The expression x and y first evaluates x ; if x is false, its value is returned; otherwise, y is evaluated and the return value is returned.
The expression x or y first evaluates x ; if x true, its value is returned; otherwise, y is evaluated and the return value is returned.
In addition, numbers equal to 0 are considered false, as are empty lines and containers. Quoting from the same document:
In the context of Boolean operations, as well as when expressions are used by flow control operators, the following values are interpreted as false: False , None , numeric zero of all types, and empty lines and containers (including lines, tuples, lists, dictionaries, sets, and freezes).
Combining these two actions means that for 0 and False 0 is considered false and is returned before evaluating the False expression. For the expression True and 0 , True is evaluated and considered a true value, therefore 0 returned. As for if and while and other Boolean operators, this result 0 also considered false.
You can use this to provide a default value, for example:
foo = bar or 'default'
To really convert a non-Boolean value to a boolean, use bool() type ; it uses the same rules as logical expressions to determine the logical value of input:
>>> bool(0) False >>> bool(0.0) False >>> bool([]) False >>> bool(True and 0) False >>> bool(1) True
To complete the image, values that are not considered false in a boolean context are considered true, including any custom classes. You can change this by implementing the .__nonzero__() special method in your class. If no such method is defined, .__len__() also be consulted. Using any of these methods, you can indicate that your type is numeric and should be considered True if it is not equal to zero, or it is a container and should be considered True if it is not empty (has a length greater than 0).