Infinity and limits
Sentinels for min/max tracking and DP initialization. Python ints never overflow, so there is no INT_MAX; use float('inf') or math.inf as the "bigger than anything" value, and compare it freely with ints.
Syntax
import math
best = math.inf # same as float("inf")
for cost in [5, 3, 8]:
best = min(best, cost)
best # -> 3
Positive and negative infinity
import math
float("inf"), float("-inf") # -> (inf, -inf)
math.inf == float("inf") # -> True
-math.inf < -10**100 # -> True, compares fine with any int
math.isinf(math.inf) # -> True
DP / distance initialization
import math
n = 5
dist = [math.inf] * n # Dijkstra / Bellman-Ford
dist[0] = 0
dp = [math.inf] * (n + 1) # coin change: min coins
ans = dp[n] if dp[n] != math.inf else -1
No integer overflow
2**100 # -> 1267650600228229401496703205376, exact
10**18 + 1 > 10**18 # -> True, arbitrary precision
# Cost: big-int ops are O(digits), not O(1)
sys.maxsize
import sys
sys.maxsize # -> 9223372036854775807 on 64-bit (2**63 - 1)
# Max container index size, NOT the max int. OK as a sentinel if you need an int.
inf gotchas
import math
math.inf - math.inf # -> nan
math.inf * 0 # -> nan
# int(math.inf) # OverflowError: inf is a float, can't convert
type(min(3, math.inf)) # -> int, min returns the actual element