Assignment Operators in Python
Assignment Operators in Python.
Assignment Operators in Python
Assignment operators are used to assign values to variables. They allow you to store and manipulate data in variables effectively. All assignment operators in python are discussed below in detail with their syntax and proper example:
1. Equal(=)
Syntax
Variable1 = Variable2 / 'Value'
Example
>>> a=10 >>> b=a >>> b 10 >>> b=20.6 >>> b 20.6 >>> b = "PrepInsta"
>>> b 'PrepInsta'
2. Plus-Equal(+=)
It adds the value or variable at right hand side to the variable at left hand side and assigns the result to the variable at left hand side.
Syntax
Variable1 += Variable2 / 'Value'
Example
>>> a=10 >>> a+=20 #equivalent to a=a+20 >>> a 30
3. Minus-Equal(-=)
It subtracts the value or variable at right hand side from the variable at left hand side and assigns the result to the variable at left hand side.
Syntax
Variable1 -= Variable2 / 'Value'
Example
>>> a=10 >>> a-=2 #equivalent to a=a-2 >>> a 8
4. Multiplication-Equal(*=)
It multiplies the value or variable at right hand side to the variable at left hand side and assigns the result to the variable at left hand side.
Syntax
Variable1 *= Variable2 / 'Value'
Example
>>> a=10 >>> a*=5 #equivalent to a=a*5 >>> a 50
5. Division-Equal(/=)
It divides the variable at left hand side by the variable or value at right hand side and assigns the result to the variable at left hand side.
Syntax
Variable1 /= Variable2 or 'Value'
Example
>>> a=10 >>> a/=5 #equivalent to a=a/5 >>> a 2
6. Modulus-Equal(%=)
It divides the variable at left hand side by the variable or value at right hand side and assigns the remainder to the variable at left hand side.
Syntax
Variable1 %= Variable2 or 'Value'
Example
>>> a=10 >>> a%=3 #equivalent to a=a%3 >>> a 1
7. Floor Division-Equal(//=)
It divides(floor division) the variable at left hand side by the variable or value at right hand side and assigns the result to the variable at left hand side.
Syntax
Variable1 //= Variable2 or 'Value'
Example
>>> a=10 >>> a//=3 #equivalent to a=a//3 >>> a 3
8. Exponential-Equal(**=)
It calculates the power of variable at left hand side raised to variable or value at the right hand side and assigns the result to the left hand side variable.
Syntax
Variable1 **= Variable2 or 'Value'
Example
>>> a=10 >>> a**=3 #equivalent to a=a**3 >>> a 1000
9. Ampersand-Equal(&=)
It performs bitwise AND operation between both the operands and assigns the result to the left hand side variable.
Syntax
Variable1 &= Variable2 or 'Value'
Example
>>> a=10 >>> a&=10 #equivalent to a=a&10 >>> a 10
10. Vertical Bar-Equal(|=)
It performs bitwise OR operation between both the operands and assigns the result to the left hand side variable.
Syntax
Variable1 |= Variable2 or 'Value'
Example
>>> a=10 >>> a|=10 #equivalent to a=a|10 >>> a 10
11. Binary Right Shift-Equal(>>=)
It performs Binary right shift operation between both the operands and assigns the result to the left hand side variable.
Syntax
Variable1 >>= Variable2 or 'Value'
Example
>>> a=10 >>> a>>=2 #equivalent to a=a>>2 >>> a 2
12. Binary Left Shift-Equal(<<=)
It performs Binary left shift operation between both the operands and assigns the result to the left hand side variable.
Syntax
Variable1 <<= Variable2 or 'Value'
Example
>>> a=10 >>> a<<=2 #equivalent to a=a<<2 >>> a 40
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment