Python 3.8 is still in development. But many alpha versions have been released. One of the latest features in Python 3.8 is the Walrus Operator. In this article, we’re going to discuss the Walrus operator and explain it with an example.
Walrus-operator is another name for assignment expressions. According to the official documentation, it is a way to assign to variables within an expression using the notation NAME := expr. The Assignment expressions allow a value to be assigned to a variable, even a variable that doesn’t exist yet, in the context of expression rather than as a stand-alone statement.
Here’s instead of using “len(a)” in two places we have assigned it to a variable called “n”, which can be used later. This helps us to resolve code duplication and improves readability.
Let’s try to understand Assignment Expressions more clearly with the help of an example using both Python 3.7 and Python 3.8. Here we have a list of dictionaries called “sample_data”, which contains the userId, name and a boolean called completed.
- Operator Overloading in Python
- Operator Functions in Python | Set 1
- Difference between == and is operator in Python
- Ternary Operator in Python
- Operator Functions in Python | Set 2
- Python | Operator.countOf
- Python | List comprehension vs * operator
- Like Operator in SAS Programming
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script