Member-only story
Python features — the walrus operator
Python 3.8 introduced the walrus operator, a feature that continues to create confusion and annoyance in equal measure. In this short article, we will try to understand what it is all about.
A new version of my book, Functional Programming in Python, is available on Amazon as a PDF or paperback.
Expressions, statements and assignment
In programming, we use the term expression to describe a short piece of code that returns a value. For example, this expression returns the sum of a and b:
a + bA statement is a short piece of code that has an effect but does not return a value. For example, this del statement removes element 2 from list k:
del k[2]Since this is a statement, it does not return a value. This makes it illegal to use a statement where a value is required. For example:
print(a + b)
print(del k[2])This code won’t even compile. The second line gives a syntax error, because print requires a value but del is a statement.
