This is where Python is added from the implicit constructor of bool() after if , because everything that follows if should be allowed to be logical. In this context, your code is equivalent
if bool("poi"): print "yes"
According to the Python constructor, bool(x) accepts something and solves the truth based on the cases below
- If x is an integer, only
0 is False everything else is True - If x is a float, only
0.0 is False everything else is True` - If x is a list, only
[] is False everything else True - If x is given / dict, only
{} is False everything else is True - If x is a tuple, only
() is False everything else True - If x is a string, only
"" is False everything else is True . Keep in mind that bool("False") will return to True
Here is the log for the cases listed above
Python 3.4.3 (default, Feb 25 2015, 21:28:45) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> bool(0) False >>> bool(1) True >>> bool(-1) True >>> bool(0.0) False >>> bool(0.02) True >>> bool(-0.10) True >>> bool([]) False >>> bool([1,2]) True >>> bool(()) False >>> bool(("Hello","World")) True >>> bool({}) False >>> bool({1,2,3}) True >>> bool({1:"One", 2:"Two"}) True >>> bool("") False >>> bool("Hello") True >>> bool("False") True
nehemiah
source share