Syntax and Builtins
Core language syntax and builtins you reach for in everyday Python. Results appear in the same fence: same-line # comments when short, multiline # blocks below the sample when not.
Busca en todas las páginas de la documentación
Core language syntax and builtins you reach for in everyday Python. Results appear in the same fence: same-line # comments when short, multiline # blocks below the sample when not.
Interpolate expressions with f-strings - the preferred string formatting style.
name = "Ada"
print(f"Hello, {name}!") # Hello, Ada!
print(f"{3.14159:.2f}") # 3.14Unpack iterables into names; use * to capture the rest.
a, b, *rest = [1, 2, 3, 4] # a=1, b=2, rest=[3, 4]
first, last = ("x", "y") # first='x', last='y'Assign inside an expression with := when it improves clarity (filters, while loops).
s = "hello"
if (n := len(s)) > 3: # n=5
print(n) # 5Pair indexes with items via enumerate; walk multiple iterables with zip.
list(enumerate(["a", "b"], start=1)) # [(1, 'a'), (2, 'b')]
list(zip([1, 2], ["x", "y"])) # [(1, 'x'), (2, 'y')]Short-circuit checks over iterables for existence or universal predicates.
all(x > 0 for x in [1, 2, 3]) # True
any(x < 0 for x in [1, -1, 2]) # True
any(x < 0 for x in [1, 2, 3]) # FalseEmpty containers, 0, None, and "" are falsy - use explicitly when needed.
bool([]) # False
bool([0]) # True
None or "fallback" # 'fallback'Structural pattern matching for tagged data (3.10+).
command = {"op": "add", "n": 3}
match command:
case {"op": "add", "n": int(n)}:
total = 10 + n # total=13
case _:
raise ValueError(command)Slices support start:stop:step - including reverse with [::-1].
nums = [0, 1, 2, 3, 4]
nums[::2] # [0, 2, 4]
nums[::-1] # [4, 3, 2, 1, 0]Sort with a key function; use reverse=True when needed.
users = [{"n": "b", "s": 1}, {"n": "a", "s": 3}]
sorted(users, key=lambda u: u["s"], reverse=True)
# [
# {'n': 'a', 's': 3},
# {'n': 'b', 's': 1},
# ]min/max accept default when the iterable may be empty (3.4+).
max([3, 9, 2], default=0) # 9
max([], default=0) # 0
min([3, 9, 2], default=0) # 2Numeric helpers for currency-ish rounding awareness and quotient/remainder.
divmod(20, 3) # (6, 2)
round(1.25, 1) # 1.2
round(2.5) # 2 (banker's rounding to even)Prefer isinstance over comparing type(x) is for polymorphism.
isinstance(3, (int, float)) # True
isinstance(True, int) # True (bool subclasses int)
isinstance("3", (int, float)) # FalseFunctional helpers exist - comprehensions are often clearer.
nums = [1, 2, 3, 4]
[n * n for n in nums if n % 2 == 0] # [4, 16]Convert between code points and characters.
ord("A") # 65
chr(65) # 'A'
ord("€") # 8364REPL discovery tools for exploring objects and modules.
"upper" in dir(str) # True
# help(list.append) # opens docs in the REPLStack versions: Python 3.14.0 (stable 3.14, maintenance 3.13) · uv 0.6+ · ruff 0.9+
Revisado por Chris St. John·Última actualización: 31 jul 2026