Lesley's Digital Garden

Home

❯

Notes

❯

Walrus Operator in Python

Walrus Operator in Python

Mar 14, 2024

  • computing/python

Python 3.8 Walrus operator makes assignment an expression. For example,

print(is_new := True)

It can be useful during while loop or if statement:

words = ['falcon', 'sky', 'ab', 'water', 'a', 'forest']
 
for word in words:
    if ((n := len(word)) < 3):
        print(f'warning, the word {word} has {n} characters')

References

  • using walrus operator in Python - ZetCode
  • The Walrus Operator: Python 3.8 Assignment Expressions – Real Python

Graph View

Backlinks

  • Python

Created with Quartz v4.5.2 © 2026. All notes are written in Obsidian.

  • GitHub Repository
  • Mastodon