On this page
bool
Prev: bin | Up: __builtins__ | Next: breakpoint
Rel: int, float, complex, str, bytes
Ref: docs.python.org — bool
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import bool
Overview & Signature
bool(object=False, /)
Returns True when the argument is true, False otherwise.
Common Methods & Attributes
bool.as_integer_ratio(self, /)— Return a pair of integers, whose ratio is equal to the original int.bool.bit_count(self, /)— Number of ones in the binary representation of the absolute value of self.bool.bit_length(self, /)— Number of bits necessary to represent self in binary.bool.conjugate(self, /)— Returns self, the complex conjugate of any int.bool.denominator(...)— the denominator of a rational number in lowest termsbool.from_bytes(bytes, byteorder='big', *, signed=False)— Return the integer represented by the given array of bytes.bool.imag(...)— the imaginary part of a complex numberbool.is_integer(self, /)— Returns True. Exists for duck type compatibility with float.is_integer.bool.numerator(...)— the numerator of a rational number in lowest termsbool.real(...)— the real part of a complex numberbool.to_bytes(self, /, length=1, byteorder='big', *, signed=False)— Return an array of bytes representing an integer.
Usage
assert bool(1) is True and bool("hi") is True
assert bool(0) is False and bool("") is False and bool([]) is False
Gotchas
- Falsy values in Python:
None,False, numeric zeros (0,0.0,0j), and empty collections ("",[],(),{},set()). - Custom classes are truthy by default unless they define
__bool__()or__len__().
REPL Inspect
help(bool)
Linked from
- tuts/python3/__builtins__
- tuts/python3/__builtins__/__builtins__
- tuts/python3/__builtins__/bin
- tuts/python3/__builtins__/breakpoint
- tuts/python3/__builtins__/bytearray
- tuts/python3/__builtins__/bytes
- tuts/python3/__builtins__/complex
- tuts/python3/__builtins__/dict
- tuts/python3/__builtins__/float
- tuts/python3/__builtins__/frozenset
- tuts/python3/__builtins__/int
- tuts/python3/__builtins__/list
- tuts/python3/__builtins__/memoryview
- tuts/python3/__builtins__/object
- tuts/python3/__builtins__/range
- tuts/python3/__builtins__/set
- tuts/python3/__builtins__/slice
- tuts/python3/__builtins__/str
- tuts/python3/__builtins__/tuple
- tuts/python3/__builtins__/type
- tuts/python3/tutorials/core_fundamentals/primitive_data_types
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bin Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/breakpoint Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytes Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/str Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytearray links to this page; shared tags: #builtins