Python Boolean Type Example

In python there are only two boolean value : True or False. And all Python object can be expressed as Boolean value, regardless of their own value. For example all python variable which value is 0’s boolean value is False, all python variable which value is not 0’s boolean value is True. Similarly, empty container object ( empty list )’s boolean value is False, but not empty container object ( not empty string )’s boolean value is True. To get an python object’s boolean value, we can use python built-in bool() function. This article will give you some example.

Open a terminal and run below source code will give you a direct impression.

$ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>


# declare a variable and assign False to it.
>>> today_is_monday = False 
# the variable's boolean value is False.
>>> bool(today_is_monday)
False


# declare a variable and assign False to it.
>>> today_is_monday = True 
# the variable's boolean value is True.
>>> bool(today_is_monday)
True


# number value is not 0, so it's boolean value is True.
>>> bool(-1.23)
True

# number 0's boolean value is False.
>>> bool(0.0)
False

# empty string's boolean value is False.
>>> bool('')
False

# not empty string's boolean value is True.
>>> bool('hello world')
True

# the list object is not empty, so it's boolean value is True.
>>> bool([0,1,None])
True

# even the list item is None, but the list is not empty, so it's boolean value is True.
>>> bool([None])
True
>>> bool([None, None])
True

# None value is same as java void or NULL value, so it's boolean value is False.
>>> bool(None)
False

# so not None's boolean value is True.
>>> bool(not None)
True

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.