0Pricing
Learn AI with Python · Lesson

Assignment Expressions (Walrus Operator)

Discover how the Walrus Operator can simplify assignments and conditionals.

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.

Assignment Expressions (Walrus Operator) — illustration 1

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}")

All lessons in this course

  1. Understanding Conditionals
  2. Boolean Logic and Comparisons
  3. Looping
  4. Assignment Expressions (Walrus Operator)
← Back to Learn AI with Python