Loading lesson path
Assignment operators are used to assign values to variables:
=
Formula
x = 5 x = 5Try it » +=
Formula
x += 3 x = x + 3Try it » -=
Formula
x -= 3 x = x - 3Try it » *=
Formula
x *= 3 x = x * 3Try it » /=
Formula
x /= 3 x = x / 3Try it » %=
Formula
x %= 3 x = x % 3Try it » //=
Formula
x //= 3 x = x // 3Try it » **=
Formula
x **= 3 x = x ** 3Try it » &=
Formula
x &= 3 x = x & 3Try it » |=
Formula
x |= 3 x = x | 3Try it » ^=
Formula
x ^= 3 x = x ^ 3Try it » >>=
Formula
x >>= 3 x = x >> 3Try it » <<=
Formula
x <<= 3 x = x << 3Try it » :=
print(x := 3)
x = 3 print(x)Try it »
Python 3.8 introduced the := operator, known as the "walrus operator". It assigns values to variables as part of a larger expression:
The count variable is assigned in the if statement, and given the value 5: numbers = [1, 2, 3, 4, 5]
if (count := len(numbers)) > 3:
print(f"List has {count} elements")