Python Glossary Русский

all Python 3Data typesterm

bool

logic

The boolean type. A subclass of int: True == 1, False == 0. bool(x) returns the truth value of x.

Syntax

bool([x])

Because bool subclasses int, True equals 1 and collides with it as a dict key ({True: 'a', 1: 'b'} keeps one entry), and True acts as 1 in arithmetic. Don't compare against True/False explicitly (if x == True) — just write if x:, or a truthy value like 2 (which is != True) will surprise you.

Examples

print(True, False)  # → True False
print(5 > 3)  # → True
print(True and False)  # → False
print(bool(0), bool(1), bool(""))  # → False True False
print(bool([]), bool([1]))  # → False True — пустое ложно, непустое истинно
print(True + True)  # → 2 — bool является подтипом int
print(not True)  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy