Note Only triggers on null/undefined, unlike || which triggers on all falsy values (0, "", false, NaN). Use ?? when 0 or empty string are valid values.
data = [5, 12, 3, 18, 7, 22]
high = [x for x in data if (doubled := x * 2) > 20]
print(high)
import re
text = "Order #12345 confirmed"if (match := re.search(r"#(\d+)", text)):
print(f"Order ID: {match.group(1)}")
output
[12, 18, 22]
Order ID: 12345
Note The walrus operator (Python 3.8+) assigns and returns a value in one step. Most useful in while-loops, if-statements, and comprehensions to avoid repeated computation.
JS: 'My cat and your dog'.match(/cat|dog/g)
Py: re.findall(r'\b(?:jpg|png|gif)\b', 'files: logo.png and bg.jpg')
output
JS: ["cat", "dog"]
Py: ["png", "jpg"]
Note The | operator has very low precedence -- /ab|cd/ matches 'ab' or 'cd', NOT 'a(b|c)d'. Always wrap alternations in a group when they are part of a larger pattern to avoid surprises.
// 'in' narrows based on the presence of a property
Note The 'in' operator checks if a property name exists on an object at runtime. TypeScript uses this to narrow union types. The property name must be a string literal for narrowing to work. Works well when union members have distinct unique properties.