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
Here:
-
xgets value10 -
ygets value5
3. Add and Assign (+=)
Adds a value and stores the result in the same variable.
Example
Output:
This is the same as:
4. Subtract and Assign (-=)
Subtracts a value and stores the result.
Example
Output:
5. Multiply and Assign (*=)
Multiplies and stores the result.
Example
Output:
6. Divide and Assign (/=)
Divides and stores the result.
Example
Output:
7. Modulus and Assign (%=)
Stores the remainder.
Example
Output:
8. Power and Assign (**=)
Raises power and stores result.
Example
Output:
9. Floor Divide and Assign (//=)
Divides and removes decimal part.
Example
Output:
10. Assignment Operators with Real Example
Example
11. Common Beginner Mistakes
❌ Forgetting to Initialize Variable
❌ Error: variable not defined.
✔ Correct:
❌ Confusing = and ==
✔ Correct:
12. Simple Practice Examples
Example 1: Increase Score
Example 2: Reduce Balance
Example 3: Square Number
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 😊
