- We can use assignment operator to assign value to the variable.
$$x = 10$$
- We can combine assignment operator with some other operator to form compound assignment operator.
$$ x += 10$$
The following is the list of all possible compound assignment operators in Python.
Assignment Operators | Example |
+= | x += 1 or x = x + 1 |
-= | x -= 1 or x = x – 1 |
*= | x *= 1 or x = x * 1 |
/= | x /= 1 or x = x / 1 |
%= | x %= 10 or x = x % 10 |
//= | x //= 11 or x = x // 11 |
**= | x **= 5 or x = x ** 5 |
&= | x &= 9 or x = x & 9 |
|= | x |= 1 or x = x | 1 |
^= | x ^= 1 or x = x ^ 1 |
>>= | x >>= 1 or x = x >> 1 |
<<= | x <<= 1 or x = x << 1 |
Example
Python
num1 = 10
num1 += 20
print(num1)
Output
PowerShell
30
Python
num1 = 10
num1 &= 5
print(num1)
Output
PowerShell
0
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!