bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Python/Foundations
Python•Foundations

Python Assignment Operators

Assignment Operators

Assignment operators are used to assign values to variables:

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
=x= 3x = x3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
:=print(x := 3)x = 3 print(x)

The Walrus Operator

Python 3.8 introduced the := operator, known as the "walrus operator". It assigns values to variables as part of a larger expression:

Example

numbers = [1, 2, 3, 4, 5]

if (count := len(numbers)) > 3:

  print(f"List has {count} elements")

Previous

Python Arithmetic Operators

Next

Python Comparison Operators