Python Assignment Operators

1. What is an Assignment Operator?

An assignment operator is used to give a value to a variable.

In simple words:
👉 It puts a value inside a variable.

2. Simple Assignment (=)

The most common assignment operator is =.

Example

x = 10
y = 5

Here:

  • x gets value 10

  • y gets value 5

3. Add and Assign (+=)

Adds a value and stores the result in the same variable.

Example

x = 10
x += 5
print(x)

Output:

15

This is the same as:

x = x + 5

4. Subtract and Assign (-=)

Subtracts a value and stores the result.

Example

x = 10
x -= 3
print(x)

Output:

7

5. Multiply and Assign (*=)

Multiplies and stores the result.

Example

x = 4
x *= 2
print(x)

Output:

8

6. Divide and Assign (/=)

Divides and stores the result.

Example

x = 10
x /= 2
print(x)

Output:

5.0

7. Modulus and Assign (%=)

Stores the remainder.

Example

x = 10
x %= 3
print(x)

Output:

1

8. Power and Assign (**=)

Raises power and stores result.

Example

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

Output:

8

9. Floor Divide and Assign (//=)

Divides and removes decimal part.

Example

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

Output:

3

10. Assignment Operators with Real Example

Example

total = 100

total += 50
total -= 20
total *= 2

print(total)

11. Common Beginner Mistakes

❌ Forgetting to Initialize Variable

x += 5

❌ Error: variable not defined.

✔ Correct:

x = 0
x += 5

❌ Confusing = and ==

if x = 5:

✔ Correct:

if x == 5:

12. Simple Practice Examples

Example 1: Increase Score

score = 10
score += 5
print(score)

Example 2: Reduce Balance

balance = 1000
balance -= 250
print(balance)

Example 3: Square Number

num = 4
num **= 2
print(num)

13. Summary (Assignment Operators)

✔ Assignment operators store values
= assigns value
+= -= *= /= update value
✔ Makes code shorter and cleaner

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Comparison Operators

  • Logical Operators

  • Operator Exercises

  • MCQs with answers

Just tell me 😊

You may also like...