[Tutor] What is the difference between checking false?

Jim Mooney cybervigilante at gmail.com
Sun Jun 16 03:30:04 CEST 2013


##This is puzzling me. If I check the equality of 0, None, empty
string, and empty list with False,
##only zero satisfies the equality. But if I use them in a not
statement, they all turn out False.
##What gives?

#Using C:\Python33\python.exe on Win 7 in c:\python33\jimprogs

print('Zero is equal to False' if 0 == False else 'Zero is NOT equal to False')
print('None is equal to false' if None == False else 'None is NOT
equal to false')
print('Empty string is equal to False' if '' == False else 'Empty
string is NOT equal to False')
print('Empty list is equal to false' if [] == False else 'Empty list
is NOT equal to false')
print()
print('Zero is equal to False' if not 0 else 'Zero is NOT equal to False')
print('None is equal to false' if not None else 'None is NOT equal to false')
print('Empty string is equal to False' if not '' else 'Empty string is
NOT equal to False')
print('Empty list is equal to False' if not [] else 'Empty list is NOT
equal to false')

##Results:
##
##Zero is equal to False
##None is NOT equal to false
##Empty string is NOT equal to False
##Empty list is NOT equal to false
##
##Zero is equal to False
##None is equal to false
##Empty string is equal to False
##Empty list is equal to False

-- 
Jim
After indictment the bacon smuggler was put on the no-fry list


More information about the Tutor mailing list