8 months, 3 weeks

Assignment expressions





Expressions and statements are the key building blocks of almost every programming language. The difference between them is really simple: expressions have a value, while statements do not.

Think of statements as consecutive actions or instructions that your program executes. So, value assignments, if clauses, together with for and while loops, are all statements. Function and class definitions are statements, too.

Think of expressions as anything that can be put into an if clause. Typical examples of expressions are literals, values returned by operators (excluding in-place operators), and comprehensions, such as list, dictionary, and set comprehensions. Function calls and method calls are expressions, too.

Python 3.8 introduced the dedicated := operator, which assigns a value to the variable but acts as an expression instead of a statement. Due to its visual appearance, it was quickly nicknamed the 
walrus operator.

The most common scenario for the walrus operator is when a complex value needs to be evaluated and then immediately used in the statements that follow.


Responses(0)







Related