Python bool() built-in function

From the Python 3 documentation

Return a Boolean value, True or False. x is converted using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise, it returns True. The bool class is a subclass of int. It cannot be subclassed further. Its only instances are False and True.

Introduction

The bool() function in Python is a built-in function that converts a value to a Boolean (True or False). It follows the standard truth testing procedure, where values like 0, None, and empty collections are considered False, while most other values are True. This is fundamental for controlling the flow of your program with conditional statements.

Examples

Falsy Values

These values are considered False:

bool(False)
bool(None)
bool(0)
bool(0.0)
bool('')      # empty string
bool([])      # empty list
bool({})      # empty dict
bool(set())   # empty set
False
False
False
False
False
False
False
False

Truthy Values

Most other values are considered True:

bool(True)
bool(1)
bool(-1)
bool('hello')
bool([1, 2])
bool({'a': 1})
True
True
True
True
True
True
Morty Proxy This is a proxified and sanitized view of the page, visit original site.