How do I use a Boolean in Python?

后端 未结 7 1648
栀梦
栀梦 2020-12-02 12:31

Does Python actually contain a Boolean value? I know that you can do:

checker = 1
if checker:
    #dostuff

But I\'m quite pedantic and enjo

7条回答
  •  旧时难觅i
    2020-12-02 13:30

    The boolean builtins are capitalized: True and False.

    Note also that you can do checker = bool(some_decision) as a bit of shorthand -- bool will only ever return True or False.

    It's good to know for future reference that classes defining __nonzero__ or __len__ will be True or False depending on the result of those functions, but virtually every other object's boolean result will be True (except for the None object, empty sequences, and numeric zeros).

提交回复
热议问题