After completing this chapter, you will be able to:
- Understand Assignment Operators.
- Explain why assignment operators are used.
- Learn different assignment operators.
- Perform compound assignments.
- Write programs using assignment operators.
- Understand the advantages of compound assignments.
Assignment operators are used to assign values to variables.
Besides assigning values, Python also provides compound assignment operators that perform an operation and assignment in a single statement.
For example,
Instead of writing
x = x + 5we can write
x += 5This makes the code shorter, cleaner, and easier to understand.
An Assignment Operator is an operator used to assign a value or the result of an expression to a variable.
Assignment operators are used to:
- Store values in variables.
- Update variable values.
- Simplify mathematical operations.
- Reduce code length.
- Improve readability.
| Operator | Description | Example |
|---|---|---|
| = | Assign | x = 10 |
| += | Add and Assign | x += 5 |
| -= | Subtract and Assign | x -= 5 |
| *= | Multiply and Assign | x *= 5 |
| /= | Divide and Assign | x /= 5 |
| //= | Floor Divide and Assign | x //= 5 |
| %= | Modulus and Assign | x %= 5 |
| **= | Exponent and Assign | x **= 2 |
Assigns a value to a variable.
number = 100
print(number)100
Adds the right operand to the left operand and stores the result.
number = 20
number += 10
print(number)30
Subtracts the right operand from the left operand and stores the result.
number = 20
number -= 5
print(number)15
Multiplies the variable by a value and stores the result.
number = 10
number *= 5
print(number)50
Divides the variable by a value and stores the result.
number = 20
number /= 4
print(number)5.0
Performs floor division and stores the result.
number = 25
number //= 4
print(number)6
Stores the remainder after division.
number = 25
number %= 4
print(number)1
Raises the variable to the specified power and stores the result.
number = 5
number **= 2
print(number)25
Write a Python program to demonstrate all assignment operators.
number = 20
print("Original Value :", number)
number += 5
print("After += :", number)
number -= 3
print("After -= :", number)
number *= 2
print("After *= :", number)
number /= 4
print("After /= :", number)
number //= 2
print("After //= :", number)
number %= 3
print("After %= :", number)
number **= 2
print("After **= :", number)Original Value : 20
After += : 25
After -= : 22
After *= : 44
After /= : 11.0
After //= : 5.0
After %= : 2.0
After **= : 4.0
START
Create variable
Apply each assignment operator
Display the updated value
STOP
Each assignment operator performs an operation and immediately stores the updated result back into the same variable.
Assignment operators are commonly used in:
- Banking Applications
- Shopping Cart Systems
- Payroll Software
- Student Result Processing
- Inventory Management
- Scientific Calculations
- Reduces code length.
- Improves readability.
- Makes code easier to maintain.
- Performs assignment and operation in one step.
| = | += |
|---|---|
| Assigns a new value | Adds and updates the existing value |
| Example: x = 10 | Example: x += 5 |
❌ Incorrect
if number = 10:
print(number)Output
SyntaxError
✅ Correct
if number == 10:
print(number)number = 10
number += 5
print(number)Output
15
The original value is updated.
Assignment operators assign values or expression results to variables.
It adds a value to the existing variable and stores the updated result.
=assigns a value.+=adds a value and updates the variable.
- =
- +=
- -=
- *=
- /=
- //=
- %=
- **=
They reduce code length, improve readability, and make programs easier to maintain.
=→ Assign+=→ Add and Assign-=→ Subtract and Assign*=→ Multiply and Assign/=→ Divide and Assign//=→ Floor Divide and Assign%=→ Modulus and Assign**=→ Exponent and Assign
In this chapter, you learned:
- What Assignment Operators are.
- Types of Assignment Operators.
- Practical examples.
- Compound assignments.
- Real-world applications.
- Common mistakes.
- Interview questions.