Python Reference ยท Built-in Functions
Python built-in constants
Reference for Python's built-in constants: True, False, None, Ellipsis (...), NotImplemented and __debug__, with examples, identity checks and pitfalls.
Python has a handful of constants that are always available and cannot be reassigned: True, False, None, Ellipsis (written ...), NotImplemented and __debug__. The first three are keywords; Ellipsis and NotImplemented are singleton objects in the built-in namespace. Because each exists exactly once, compare them with is.
The site module also adds quit, exit, copyright, credits and license for interactive sessions. They are not available when Python runs with -S and should not be used in programs.
x is None x is not True
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Reference
| Constant | Type | Meaning |
|---|---|---|
True |
bool |
Boolean true; equal to 1. |
False |
bool |
Boolean false; equal to 0. |
None |
NoneType |
The absence of a value. Returned by functions with no return. |
Ellipsis / ... |
ellipsis |
A placeholder used in slicing (NumPy), type hints and stub bodies. |
NotImplemented |
NotImplementedType |
Returned by binary special methods to say "I don't support this operand". |
__debug__ |
bool |
True unless Python runs with -O; controls assert. |
Usage notes
- Use
isforNone,TrueandFalsechecks that matter:if x is None. Plain truthiness (if not x) also catches0,""and[], which is often a bug whenNonehas a special meaning. boolis anintsubclass.True + 1is2, andsum()over booleans counts them.True == 1butTrue is not 1.Noneis falsy, has no attributes except the usual object ones, and is the default return value. It cannot be assigned to....is a real value.def f(): ...is a legal empty body, handy in stubs, protocols and abstract methods. Preferpassfor "intentionally nothing".NotImplementedis not an exception. Return it (do not raise it) from__eq__,__add__and similar so Python can try the other operand's reflected method. RaisingNotImplementedErroris the different tool for abstract methods. UsingNotImplementedin a boolean context is deprecated (aDeprecationWarningsince 3.9; an error in 3.14).__debug__cannot be assigned.assertstatements are removed when optimization is on.- Related singletons: an empty
tuple(), small integers and some strings may be shared, but never rely onisfor them.
Related
bool(),object(),isinstance(),slice(), special methods- Lessons: Booleans, None, Special methods