On 28/07/13 04:03, Jim Mooney wrote:
I did this a while back and it took me some time to figure why I got
opposite results:
if [True]: print(True)
else: print(False)
# result: True
[True] is a non-empty list, and as a non-empty list, it is a truthy (true-like) value.
You could have written [42], or ["hello world"], or even [False] or [0]. What
matters is that the list is non-empty and therefore truthy.
if [True] == True: print(True)
else: print(False)
# result: False
Obviously a list of one item is not equal to True, no matter what that item
happens to be.
Likewise [101] != 101, ["spam"] != "spam", and [[]] != []. A list containing an
empty list is not equal to an empty list.
However, this will test your knowledge:
L = []
L.append(L)
[L] == L
True or false? Can you explain why?
--
Steven
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor