Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Inplace vs Standard Operators in Python
Python provides two types of operators: inplace operators that modify variables directly, and standard operators that create new values. Understanding the difference helps you write more efficient code.
Inplace Operators
Inplace operators modify the original variable without creating a copy. They use compound assignment syntax like +=, -=, etc.
Basic Example
a = 9 a += 2 print(a)
11
The += operator adds 2 to the original value of a and updates it in place.
Common Inplace Operators
-
+=(addition) -
-=(subtraction) -
*=(multiplication) -
/=(division) -
%=(modulo)
String Concatenation
language = "Python" language += "3" print(language)
Python3
Using operator Module for Inplace Operations
Python's operator module provides functions equivalent to inplace operators. Each x += y operation is equivalent to operator.iadd(x, y).
iadd() - Addition
import operator
x = operator.iadd(9, 18)
print("Result after adding:", x)
Result after adding: 27
isub() - Subtraction
import operator
x = operator.isub(9, 18)
print("Result after subtraction:", x)
Result after subtraction: -9
imul() - Multiplication
import operator
x = operator.imul(9, 18)
print("Result after multiplying:", x)
Result after multiplying: 162
iconcat() - String Concatenation
import operator
x = "Tutorials"
y = "Point"
result = operator.iconcat(x, y)
print("After concatenation:", result)
After concatenation: TutorialsPoint
Standard Operators
Standard operators create new values without modifying the original operands. They include arithmetic, comparison, logical, and other operator types.
Types of Standard Operators
-
Arithmetic Operators:
+,-,*,/,%,**,// -
Comparison Operators:
==,!=,>,<,>=,<= -
Logical Operators:
and,or,not -
Bitwise Operators:
&,|,^,~,<<,>> -
Membership Operators:
in,not in -
Identity Operators:
is,is not
Example
a = 9
b = 18
result = a + b # Standard addition - creates new value
print("a:", a)
print("b:", b)
print("result:", result)
a: 9 b: 18 result: 27
Operator Module Functions
The operator module provides function equivalents for all Python operators:
| Operation | Syntax | Function |
|---|---|---|
| Addition | x + y | add(x, y) |
| Subtraction | x - y | sub(x, y) |
| Multiplication | x * y | mul(x, y) |
| Division | x / y | truediv(x, y) |
| Equality | x == y | eq(x, y) |
| Less than | x < y | lt(x, y) |
Comparison: Inplace vs Standard
| Aspect | Inplace Operators | Standard Operators |
|---|---|---|
| Memory Usage | More efficient | Creates new objects |
| Original Value | Modified | Unchanged |
| Syntax | x += y | x = x + y |
| Use Case | When you want to update | When you want new value |
Conclusion
Use inplace operators (+=, -=) when you want to modify variables directly for better memory efficiency. Use standard operators (+, -) when you need to preserve original values and create new results.
