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
But, you can combine arithmetic operators with the assignment operator. Let's see how this works
num = 2
num += 8
print(num)
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)
The multiplication operator turns into the multiply-equal operator:
num = 10
num *= 5
print(num)
The division operator turns into the divide-equal operator:
num = 10
num /= 5
print(num)
The exponential operator turns into the power-equal operator:
num = 2
num **= 3
print(num)
The floor division operator turns into the floor-divide-equal operator:
num = 10
num //= 3
print(num)
And last but not least the modulo operator turns into the modulo-equal operator:
num = 10
num %= 3
print(num)