Skip to main content
A literal in Python is a lexical token that represents a fixed, constant value of a built-in type directly embedded within the source code. According to the Python Language Reference, strict lexical literals are limited to numeric values and string or bytes sequences. They evaluate to themselves and represent immutable data at the parsing stage.

Numeric Literals

Numeric literals represent scalar mathematical values. Python’s lexical analyzer parses these into three distinct numeric types. All numeric literals support the use of underscores (_) to group digits for enhanced readability.
  • Integer Literals: Whole numbers. They can be expressed in standard base-10 (decimal) or prefixed to represent base-2 (0b or 0B), base-8 (0o or 0O), or base-16 (0x or 0X).
  • Floating-Point Literals: Real numbers containing a fractional component. They are defined using a decimal point or scientific notation using e or E to denote the power of 10.
  • Imaginary Literals: The imaginary component of a complex number, denoted by appending a j or J to a decimal or floating-point numeric value. Python does not have complex literals; complex numbers are expressions formed by adding a real number to an imaginary literal.

String and Bytes Literals

String literals represent sequences of Unicode characters, while bytes literals represent sequences of 8-bit values.
  • String Literals: Enclosed in single ('), double ("), or triple (''' or """) quotes. They support prefixes that alter lexical parsing, such as r or R for raw strings (disabling escape sequence processing) and f or F for formatted string literals (allowing expression interpolation).
  • Bytes Literals: Prefixed with b or B. They produce an instance of the bytes type rather than the str type and may only contain ASCII characters.

Constant Keywords (Colloquial Literals)

While frequently referred to as “boolean literals” or “null literals,” Python strictly categorizes True, False, and None as reserved keywords rather than lexical literals. They represent immutable, singleton objects in memory.

Displays and Enclosures (Collection “Literals”)

Developers commonly use the term “literal” to describe the inline syntax for creating collections (e.g., “list literal”). However, the Python Language Reference categorizes these as displays or enclosures. Unlike strict literals, collection displays evaluate to new, often mutable, objects in memory each time the expression is executed.
  • List Displays: Ordered, mutable sequences enclosed in square brackets [].
  • Tuple Displays: Ordered, immutable sequences constructed by the comma operator (e.g., 1, 2, 3). Parentheses () are optional but frequently used for grouping to avoid syntactic ambiguity. Parentheses are strictly required only to denote an empty tuple (). A trailing comma is required to construct a single-element tuple (e.g., 1,).
  • Dictionary Displays: Ordered (as of Python 3.7), mutable mappings of key-value pairs enclosed in curly braces {}.
  • Set Displays: Unordered collections of unique, hashable elements enclosed in curly braces {}. Critical Caveat: An empty pair of curly braces {} evaluates to an empty dictionary, not an empty set. To create an empty set, the set() constructor must be used.
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More