Operators I

Assignment Operators in Python

Learn about Assignment Operators in Python.


Now that you know about arithmetic operators in Python, let's talk about another operator: the assignment operator.

It's actually nothing new to you. The assignment operator is the one used to assign a value to a variable:

num = 2
Python

But, you can combine arithmetic operators with the assignment operator. Let's see how this works

num = 2 num += 8 print(num)
Python
Output

Here, the combination of + and = adds 8 to the already existing value of num.

You can combine the assignment operator = with all arithmetic operators that you already now.

The subtraction operator turns into the minus-equal operator:

num = 10 num -= 5 print(num)
Python
Output

The multiplication operator turns into the multiply-equal operator:

num = 10 num *= 5 print(num)
Python
Output

The division operator turns into the divide-equal operator:

num = 10 num /= 5 print(num)
Python
Output

The exponential operator turns into the power-equal operator:

num = 2 num **= 3 print(num)
Python
Output

The floor division operator turns into the floor-divide-equal operator:

num = 10 num //= 3 print(num)
Python
Output

And last but not least the modulo operator turns into the modulo-equal operator:

num = 10 num %= 3 print(num)
Python
Output