=) Be Used in Expressions in Python?In Python, the assignment operator (=) is not an expression but a statement. This means it does not return a value, so using it inside expressions (like if or while conditions) will cause a SyntaxError.
= in Expressions (SyntaxError)if (x = 10): # ❌ SyntaxError
print(x)
🔴 Error Output:
SyntaxError: invalid syntax
✅ Fix: You must assign x outside the if condition.
x = 10
if x > 5:
print(f"x is {x}") # ✅ Works correctly
:=) InsteadPython 3.8 introduced the walrus operator (:=), which allows assignment within expressions.
✅ Correct Usage with :=
if (x := 10) > 5:
print(f"x is {x}") # Output: x is 10
💡 Why does this work?
x := 10 assigns 10 to x and returns 10, which is then used in the if condition.:= in while LoopsBefore Python 3.8, you had to assign values outside the loop.
data = input("Enter something: ")
while len(data) > 0:
print(f"You entered: {data}")
data = input("Enter something: ")
🔴 Problem: input() is called twice.
✅ Using Walrus Operator (:=) to Simplify
while (data := input("Enter something: ")): # Stops when input is empty
print(f"You entered: {data}")
✔ Cleaner and avoids calling input() twice.
:= in List ComprehensionsBefore :=, you had to write:
numbers = [10, 20, 30, 40]
squared = [num ** 2 for num in numbers if num ** 2 > 500]
🔴 Problem: num ** 2 is computed twice.
✅ With := (Avoids Recalculation)
numbers = [10, 20, 30, 40]
squared = [square for num in numbers if (square := num ** 2) > 500]
print(squared) # Output: [900, 1600]
✔ Efficient because square is computed once.
For more details, see Walrus Operator (:=) in Python