The Walrus Operator
Python is a living language. With every new release the Python development team adds new features, improves existing ones and occasionally removes things that are no longer needed. Keeping up with these changes is part of being a developer and the best place to do that is the official Python documentation:
Python 3.14 Official Documentation
The walrus operator := was introduced
in Python 3.8 and is one of the more useful recent
additions to the language. It is called the walrus operator because
:= looks like the eyes and tusks of a walrus on its side.
What Does the Walrus Operator Do?
The walrus operator is also known as the
assignment expression. It allows you to
assign a value to a variable and use that variable in the
same expression at the same time. This is something the
regular = assignment operator cannot do.
Without the walrus operator you often find yourself writing two separate lines, one to assign a value and one to use it. The walrus operator lets you combine them into one.
A simple example without the walrus operator:
The same example using the walrus operator:
Both produce the same result but the walrus operator version is
more concise. The value is assigned to n and
immediately used in the condition in a single expression.
Walrus Operator with a while Loop
One of the most common and practical uses of the walrus operator
is inside a while loop. Instead of assigning a value
before the loop and updating it inside, you can do both in the
loop condition itself.
The walrus operator assigns the user input to user_input
and immediately evaluates whether it equals "quit" in
the same line. Clean, concise and easy to read.
Walrus Operator with a List Comprehension
The walrus operator is also useful inside list comprehensions when you need to perform a calculation and use the result in both the condition and the output without calculating it twice.
Without the walrus operator you would either need to calculate the value twice or use a regular loop instead. The walrus operator keeps it in one clean expression.
Walrus Operator with a Function
You can also use the walrus operator to assign and check the result of a function call in a single line. This avoids calling the function twice just to store and then check the result.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named new_features.py in your part56 folder.
- For each exercise you will need to research the feature in the official Python documentation before you can complete it.
- Python - What's New
Exercise 1: match / case (Python 3.10)
-
Research: Python 3.10 introduced structural pattern
matching using the
matchandcasekeywords. Find out how it works and how it compares to a chain ofif,elifandelsestatements. -
Task: Create a variable
command = "quit". -
Goal: Use
matchandcaseto print a different message for each of the following commands:"start","stop","quit"and a default case for anything else. Write a comment explaining howmatch / casediffers from a chain ofif / elif / elsestatements.
Exercise 2: Exception Groups (Python 3.11)
-
Research: Python 3.11 introduced
ExceptionGroupand theexcept*syntax which allows you to handle multiple exceptions at the same time. Find out how it works in the official documentation. -
Task: Create a simple
ExceptionGroupcontaining aValueErrorand aTypeError. -
Goal: Use
except*to handle each exception type separately and print a message for each one. Write a comment explaining when this feature would be useful compared to a regulartry / exceptblock.
Exercise 3: Improved f-strings (Python 3.12)
- Research: Python 3.12 significantly improved f-strings allowing things that previously caused syntax errors. Find out what changed and what you can now do inside f-string curly braces that was not possible before.
-
Task: Create a list
scores = [45, 92, 78, 55, 88]. - Goal: Use the new f-string capabilities to print the maximum score, the minimum score and a sorted version of the list all inside a single f-string expression. Write a comment explaining what previously would have caused a syntax error that now works in 3.12.
Exercise 4: Type Parameter Syntax (Python 3.12)
-
Research: Python 3.12 introduced a new cleaner
syntax for generic type parameters using square brackets directly
on a function or class definition. Find out how the new
typestatement works and how it simplifies type hints. - Task: Write a simple generic function using the new type parameter syntax introduced in 3.12.
- Goal: Write a comment explaining what type hints are, why they are useful in larger codebases and how the new syntax in 3.12 improves on the previous approach.
Exercise 5: Your Choice
- Research: Browse the official Python What's New pages for versions 3.10 through to the latest release and find a feature that interests you that has not been covered in the previous exercises.
- Goal: Write a working example of the feature in VS Code. Write a comment explaining what the feature does, which version it was introduced in and why you found it interesting. Be prepared to explain it to someone who has never heard of it.
Don't Forget to commit and Push!