Python Assignment Expressions and Using the Walrus Operator (Summary)
You now know how the new walrus operator works and how you can use it in your own code. By using the :=
syntax, you can avoid different kinds of repetition in your code and make your code both more efficient and easier to read and maintain. At the same time, you shouldn’t use assignment expressions everywhere. They will only help you in some use cases.
In this course, you learned how to:
- Identify the walrus operator and understand its meaning
- Understand use cases for the walrus operator
- Avoid repetitive code by using the walrus operator
- Convert between code using the walrus operator and code using other assignment methods
- Understand the impacts on backward compatibility when using the walrus operator
- Use appropriate style in your assignment expressions
To learn more about the details of assignment expressions, see PEP 572. You can also check out the PyCon 2019 talk PEP 572: The Walrus Operator, where Dustin Ingram gives an overview of both the walrus operator and the discussion around the new PEP.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:03 You now know how the walrus operator works and how you can use it in your own code. By using the walrus operator, you can avoid different kinds of repetition and make your code both more efficient and easier to read and maintain. At the same time, you shouldn’t use assignment expressions everywhere.
00:21 They’ll only help you in some use cases. In this course, you learned how to identify the walrus operator and understand its meaning, understand use cases for the walrus operator, avoid repetitive code by using the walrus operator, convert between code using the walrus operator and code using other assignment methods, understand the impacts on backwards compatibility when using the walrus operator, and use appropriate style in your assignment expressions.
00:54 We hope you found this course useful, and we’ll see you again soon at realpython.com.
Become a Member to join the conversation.