SyntaxStudy
Sign Up
Python Constants and Walrus Operator
Python Intermediate 8 min read

Constants and Walrus Operator

Constants and the Walrus Operator

Constants

Python has no built-in constant type — by convention, use ALL_CAPS.

MAX_SIZE = 1000
PI = 3.14159
DB_URL = "localhost"

Walrus Operator (:=)

Introduced in Python 3.8, assigns and returns a value in one expression.

# Without walrus
data = get_data()
if data:
    process(data)

# With walrus
if data := get_data():
    process(data)
while chunk := file.read(8192):
    process(chunk)
Pro Tip

The walrus operator reduces repetition in while loops and comprehensions.