Lessons
Courses
Notes in current language not found. Defaulting to English.

Assignment Operators in Python [ English ]

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

OperatorExampleMeaning
=x = 5Assign value
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
//=x //= 2x = x // 2
%=x %= 2x = x % 2
**=x **= 2x = x ** 2

3. Basic Assignment (=)

Assigns a value to a variable.

x = 10print(x)

Output

10

4. Add and Assign (+=)

x = 5x += 3print(x)

Output

8

5. Subtract and Assign (-=)

x = 10x -= 4print(x)

Output

6

6. Multiply and Assign (*=)

x = 6x *= 2print(x)

Output

12

7. Divide and Assign (/=)

x = 10x /= 2print(x)

Output

5.0

8. Floor Divide and Assign (//=)

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

Output

3

9. Modulus and Assign (%=)

x = 10x %= 3print(x)

Output

1

10. Exponent and Assign (**=)

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

Output

8

11. Multiple Assignment

Python allows assigning multiple variables at once.

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

12. Assigning Same Value

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

13. Practical Example

total = 0 total += 10total += 20total += 30 print("Total:", total)

Output

Total: 60

14. Important Points

  • Assignment operators store and update values
  • Short-hand operators (+=, -=, etc.) make code short and efficient
  • /= always returns a float value
  • Can be used with different data types

15. Summary

  • Assignment operators are used to assign and update values

  • Types include:

    • =, +=, -=, *=, /=, //=, %= , **=
  • Help in writing clean and concise code

  • Support multiple assignments