
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inplace vs Standard Operators in Python
Inplace Operator in Python
Inplace operation is an operation which directly changes the content of a given linear algebra or vector or metrices without making a copy. Now the operators, which helps to do this kind of operation is called in-place operator.
Let’s understand it with an a simple example -
a=9 a += 2 print(a)
output
11
Above the += tie input operator. Here first, a add 2 with that a value is updated the previous value.
Above principle applies to other operators also. Common in place operators are -
- +=
- -=
- *=
- /=
- %=
Above principle applies to other types apart from numbers, for example -
language = "Python" language +="3" print(language)
Output
Python3
Above example of x+=y is equivalent to x = operator.iadd(x,y)
There are multiple operators which are used for inplace operations.
iadd()
This function is used to assign the current value and add them. This operator does x+=y operation.
x =operator.iadd(9,18) print("Result after adding: ", end="") print(x)
Result
Result after adding: 27
isub()
This function is used to assign the current value and subtract them. Isub() function does x-=y operation.
x =operator.isub(9,18) print("Result after subtraction: ", end="") print(x)
Result
Result after subtraction: -9
imul()
This function is used to assign the current value and multiply them. This operator does x*=y operation.
x =operator.imul(9,18) print("Result after multiplying: ", end="") print(x)
Result
Result after multiplying: 162
itruediv()
This function is used to assign the current value and divide them. This operator does x/=y operation.
x =operator.itruediv(9,18) print("Result after dividing: ", end="") print(x)
Result
Result after dividing: 0.5
imod()
this function is used to assign the current value and divide them. This operator does x %=y operation.
x =operator.imod(9,18) print("Result after moduls: ", end="") print(x)
Result
Result after moduls: 9
iconcat()
This function is used to concatenate two strings.
x = "Tutorials" y = "Point" str1 = operator.iconcat(x,y) print(" After concatenation : ", end="") print(str1)
Result
After concatenation : TutorialsPoint
ipow()
This function is equivalent to x **=y.
x =operator.ipow(2,6) print("Result after exponent: ", end="") print(x)
Result
Result after exponent: 64
Standard Operators
Operators are the constructs which can manipulate the value of operands.
For example in the expression- 9+18 = 27, here 9 and 18 are operands and + is called operator.
Types of Operator
Python language supports the following types of operators -
Arithmetic Operators: (for example: +, -, *, /, %, **, //)
Comparision Operators: (for example: “==”, “!=”, “<>”, “>”, “<”, “>=”, “<=”)
Assignment Operators: (for example: “=”, “+=”, “-=”, “*=”, “/=”, “%=”, “**=”, “//=”)
Logical Operators: (for example: “Logical AND”, “Logical OR”, “Logical NOT”)
- Bitwise Operators: (for example: “|”, “&”, “^”, “~”, “<<”, “>>”)
Membership Operators: (for example: in, not in)
Identity Operators: (for example: is, is not)
Mapping Operators to Functions
Below is a table showing how abstract operations correspond to operator symbols in the Python syntax and the functions in the operator module.
Operation |
Syntax |
Function |
---|---|---|
Addition |
x + y |
add(x, y) |
Concatenation |
Seq1 + seq2 |
Concat(seq1, seq2) |
Containment Test |
Obj in seq |
Contains(seq, obj) |
Division |
x / y |
Truediv(x, y) |
Division |
x // y |
Floordiv(x, y) |
Bitwise And |
x & y |
And_(x, y) |
Bitwise Exclusive Or |
x ^ y |
Xor(x, y) |
Bitwise Inversion |
~x |
Invert(x) |
Bitwise Or |
x|y |
or_(x,y) |
Exponentiation |
x ** y |
pow(x, y) |
Identity |
x is y |
is_(x, y) |
Identity |
x is not y |
is_not(x, y) |
Indexed Assignment |
obj[k] = v |
setitem(obj, k, v) |
Indexed deletion |
del obj[k] |
delitem(obj, k) |
Indexing |
Obj[k] |
Getitem(obj,k) |
Left shift |
a << b |
Lshift(a,b) |
Modulo |
a % b |
Mod(a, b) |
Multiplication |
x*y |
mul(x*y) |
Matrix multiplication |
x@b |
Matmul(x, y) |
Negation(Arithmetic) |
-a |
Neg(a) |
Negation(Logical) |
not a |
not_(a) |
Positive |
+a |
pos(a) |
Right shift |
a>>b |
Rshift(a,b) |
Slice Assignment |
Seq[i:j] =values |
Setitem(seq, slice(i,j), values) |
Slice deletion |
Del seq[i:j] |
Delitem(seq, slice(i,j)) |
Slicing |
Seq[i:j] |
Getitem(seq, slice(I, j)) |
String formatting |
S % obj |
Mod(s, obj) |
Subtraction |
a-b |
Sub(a,b) |
Truth Test |
obj |
truth(obj) |
Ordering |
a<b |
lt(a,b) |
Ordering |
a<=b |
le(a,b) |
Equality |
a == b |
eq(a,b) |
Difference |
a != b |
ne(a,b) |
Ordering |
a >= b |
ge(a, b) |
Ordering |
a > b |
gt(a, b) |