How do I use a Boolean in Python?

后端 未结 7 1620
栀梦
栀梦 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条回答
  •  时光取名叫无心
    2020-12-02 13:23

    checker = None 
    
    if some_decision:
        checker = True
    
    if checker:
        # some stuff
    

    [Edit]

    For more information: http://docs.python.org/library/functions.html#bool

    Your code works too, since 1 is converted to True when necessary. Actually Python didn't have a boolean type for a long time (as in old C), and some programmers still use integers instead of booleans.

提交回复
热议问题