Notes
Categories

Assignment Operators in Python [ English ]

< Prev Next >

1. Introduction

Assignment operators in Python are used to assign values to variables. They can also modify the value of a variable by combining assignment with arithmetic operations.

2. Types of Assignment Operators

Operator Example Meaning
= x = 5 Assign value
+= x += 3 x = x + 3
-= x -= 2 x = x - 2
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2
//= x //= 2 x = x // 2
%= x %= 2 x = x % 2
**= x **= 2 x = x ** 2

3. Basic Assignment (=)

Assigns a value to a variable.

x = 10
print(x)

Output

10

4. Add and Assign (+=)

x = 5
x += 3
print(x)

Output

8

5. Subtract and Assign (-=)

x = 10
x -= 4
print(x)

Output

6

6. Multiply and Assign (*=)

x = 6
x *= 2
print(x)

Output

12

7. Divide and Assign (/=)

x = 10
x /= 2
print(x)

Output

5.0

8. Floor Divide and Assign (//=)

x = 10
x //= 3
print(x)

Output

3

9. Modulus and Assign (%=)

x = 10
x %= 3
print(x)

Output

1

10. Exponent and Assign (**=)

x = 2
x **= 3
print(x)

Output

8

11. Multiple Assignment

Python allows assigning multiple variables at once.

a, b, c = 1, 2, 3
print(a, b, c)

12. Assigning Same Value

x = y = z = 5
print(x, y, z)

13. Practical Example

total = 0

total += 10
total += 20
total += 30

print("Total:", total)

Output

Total: 60

14. Important Points

15. Summary

< Prev Next >