Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.

# Integer Literals
decimal_lit = 42
grouped_lit = 1_000_000  # Underscores for readability
binary_lit = 0b101010
octal_lit = 0o52
hex_lit = 0x2A


# Floating-Point Literals
float_lit = 3.14159
sci_float_lit = 1.5e2  # Evaluates to 150.0


# Imaginary Literals
imaginary_lit = 3j
complex_expr = 2 + 3j  # Expression combining an integer and 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.

# String Literals
single_quote_lit = 'string'
double_quote_lit = "string"
multi_line_lit = """line1
line2"""


# Prefixed String Literals
raw_string_lit = r"C:\new\folder"
f_string_lit = f"Value: {decimal_lit}"


# Bytes Literal
bytes_lit = b"ascii_data"

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.
true_kw = True
false_kw = False
null_kw = None

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.

# List Display
list_display = [1, 2, 3, "four"]


# Tuple Display
tuple_display = 1, 2, 3
grouped_tuple = (1, 2, 3)
single_element_tuple = 1,
empty_tuple = ()


# Dictionary Display
dict_display = {"key1": "value1", "key2": 2}
empty_dict = {}


# Set Display
set_display = {1, 2, 3, 3}  # Evaluates to {1, 2, 3}
empty_set = set()           # {} cannot be used to create an empty set
Master Python with Deep Grasping Methodology!Learn More