Interview Prep/Python for DevOps/Language essentials & functions

What does the walrus operator `:=` do?

IntermediateConceptLanguage essentials & functions
Answer

It assigns inside an expression, which removes duplicate calls in loops and comprehensions:

while (chunk := f.read(65536)):
    h.update(chunk)

if (m := re.search(r"ERROR (\w+)", line)):
    codes[m.group(1)] += 1

big = [s for v in volumes if (s := v["Size"]) > 500]

Use it where it removes repetition; do not chain it into unreadable one-liners.

All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions