Assignment Expressions (Walrus Operator)
Discover how the Walrus Operator can simplify assignments and conditionals.
Assignment Expressions (Walrus Operator) is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to the Walrus Operator
The Walrus Operator (:=) allows you to assign and evaluate a value in a single expression. This makes your code more concise and readable.
In this lesson, you’ll learn how to use the Walrus Operator effectively in Python.

2
What is the Walrus Operator?
The Walrus Operator (:=) is used to assign a value to a variable as part of an expression. It was introduced in Python 3.8.
For example:
# Example of Walrus Operator
if (n := len([1, 2, 3, 4])) > 3:
print(f"List length is {n}")3
4
Using the Walrus Operator in Loops
The Walrus Operator is particularly useful in loops. For example:
# Using Walrus Operator in a loop
data = [1, 2, 3, 4, 5]
while (n := len(data)) > 0:
print(f"Popping an item. {n} items left.")
data.pop()5
Benefits of the Walrus Operator
The Walrus Operator helps to:
- Make code more concise.
- Avoid redundant lines of code.
- Improve code readability.
6
Practical Example
Here’s a practical example using the Walrus Operator:
# Practical example with Walrus Operator
while user_input := input("Enter something (or press Enter to quit): "):
print(f"You entered: {user_input}")7
Limitations of the Walrus Operator
Although the Walrus Operator is powerful, it should not be overused. It can make code harder to read when used in complex expressions.
Use it only when it makes your code more concise and readable.
8
Nested Usage of the Walrus Operator
The Walrus Operator can also be used in nested structures. For example:
# Nested usage of Walrus Operator
data = [1, 2, 3]
if (length := len(data)) > 0:
print(f"The list has {length} items.")9
10
Common Mistakes
Here are some common mistakes to avoid when using the Walrus Operator:
- Using it in overly complex expressions that hurt readability.
- Using it in Python versions earlier than 3.8.
11
What Did We Learn?
In this lesson, you learned:
- What the Walrus Operator (
:=) is and how it works. - How to use it in loops and conditional expressions.
- Its benefits and limitations.
Great job! Let’s move on to the next topic.

Frequently asked questions
Is the “Assignment Expressions (Walrus Operator)” lesson free?
Yes — the full text of “Assignment Expressions (Walrus Operator)” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Assignment Expressions (Walrus Operator)”?
Discover how the Walrus Operator can simplify assignments and conditionals. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Assignment Expressions (Walrus Operator)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding Conditionals
- Boolean Logic and Comparisons
- Looping
- Assignment Expressions (Walrus Operator)