Python Glossary Русский

all Python 3Built-in typesterm

True

special values

Boolean truth, one of the two instances of bool. Since bool subclasses int, True behaves like 1: True + True == 2.

Syntax

True

True is a built-in constant: assigning to it is a SyntaxError. Because bool subclasses int, True == 1, so sum() over a list of flags counts the true ones, and True and 1 are the same dict key. The trap is x == True versus x is True: the first asks "does it equal one", the second "is it the very True object", and both reject a truthy 2 or a non-empty list; to ask "is it true", just write if x:.

Examples

print(type(True).__name__, isinstance(True, int))   # → bool True
print(True + True)   # → 2
print(sum([True, False, True]))   # → 2 — считает истинные
x = 1
print(x == True, x is True)   # → True False — равно, но не тот объект
y = 2
print(y == True, bool(y))   # → False True — истинно, но не равно True
exec('True = 1')   # → SyntaxError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy