What is the Python Walrus Operator?
Many alpha versions of the Python 3.8 have been released. Out of which the Python Walrus Operator is gaining popularity.
The assignment expressions are now written with a new notation (:=). As the notation, somehow resembles the eyes and tusks of a walrus this assignment expression is also called as Walrus-operator. We can use assignment expressions and Python walrus operators interchangeably.
With the Python walrus operator, we can assign value to a variable held within an expression using the Python walrus operator notation that is, NAME := expr. As we know, with assignment expressions we were allowed to assign and return a value to a variable in the same expression, even when the variable didn't exist in the program.
Let's see this functionality with the below example
Code:
Output:
Explanation:
As seen above, with the older version of Python we have to first assign value to the variable and then we used the print function to print the variable. Whereas with the Python walrus operator, it becomes easy to simply use the new notation called Python walrus operator or new assignment expression to assign the value and immediately print the value.
Quick Note: With the walrus operator nothing that is not possible can be executed. It just makes certain constructs more convenient to use and helps to communicate the intent of the code more clearly.
You might have a question, What happens if we try to print the variable the same way as the walrus operator without using the walrus operator?
Code:
Output:
Explanation:
As seen above, if we try using the concept of the walrus operator without exactly using the Python walrus operator then we shall encounter a TypeError. The TypeError: 'whale' is an invalid keyword argument for print() is the error message that you shall also see if you try using the concept of the Python walrus operator without exactly using the Python walrus operator. Hence, the walrus operator came into existence with a new version of Python 3.8.
Quick Note: Without implementing the walrus operator, we would need to have two lines of code. However, by implementing the walrus operator, our code can be shrunk within one line.
Example of Python Walrus Operator
Let us dive into some code examples to understand the concept around the Python walrus operator.
Example 1: A Simple Code Example
Algorithm:
- We define an empty list called "subjects" that shall all the subjects that the user shall input into the program.
- Then we use the while loop from Python, to take input from the user of the code to enter all the subjects studied so far, one by one.
- We used the Python walrus operator to assign the variable "subject" the values that the input() function In Python shall get from the users.
- Until the user enters "EXIT", with the append() function in Python all the subjects that the user enters get added t the empty list one after another.
- Lastly, with the print statement, the subjects list is given out containing all the subjects which were input by the user.
Code:
Output:
Explanation:
As seen above, we defined an empty list called "subjects" that shall all the subjects that the user shall input into the program. After which we used the walrus operator to assign the variable "subject" the values that the input() function In Python shall get from the users. inside the while loop from Python. As seen, subjects like 'Hindi', 'English', 'Sanskrit', 'Mathematics', and 'Science' are entered by the users. Once the user enters "EXIT", the program is stopped. As the append() function in Python was used all the subjects that the user enters get added t the empty list one after another and the list "subjects" with all the subjects in it is printed.
Example 2: A Code Example to Understand Why the Python Walrus Operator Is More Convenient to Use than Before
We understand how the walrus operator comes in handy and how without it the code length and complexity increase.
Algorithm:
- We define an empty list called "seasons" for both cases, which shall contain all the seasons that the user shall input into the program.
- For case 1, where we implement the walrus operator, where we use the while loop from Python, to take input from the user of the code to enter all the seasons of a year, one by one. By the Python walrus operator, we assign the variable "season" the values that the input() function In Python shall get from the users until it enters "EXIT". All the seasons are added to the empty list via the append() function in Python which is finally printed in a list.
- For case 2, where we do not implement the walrus operator, we start by setting the variable " season" to something other than 'EXIT'.
- We use the while loop from Python, to take input from the user of the code to enter all the seasons of a year until the condition of season == "EXIT" holds TRUE. All the sections are added to the empty list via the append() function in Python which is finally printed in a list.
Code:
Output:
Explanation: As seen above, definitely the code where implement the walrus operator is much easier to understand and implement.
Learn More
Some of the cool projects in Python which can help you understand concepts of Python a lot deeper along with having fun programming in Python are given below.
- Library Management System Project in Python
- Snake Game in Python
- Tic Tac Toe Python
- How to flatten a list in Python
Challenge Yourself: You can use the Python walrus operator concepts you studied in this article and implement them when building your version of the amazing Python programs.
Conclusion
- The assignment expressions are now written with a new notation (:=). As the notation, somehow resembles the eyes and tusks of a walrus this assignment expression is also called Walrus-operator.
- If we try using the concept of the walrus operator without exactly using the Python walrus operator, then we shall encounter a TypeError: 'variable' is an invalid keyword argument for print().
- walrus operator is a part of the new functionality with the latest version of Python 3.8.
- With the walrus operator nothing that is not possible can be executed. It just makes certain constructs more convenient to use and helps to communicate the intent of the code more clearly.