Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Assignment Operators in Python

Assignment Operators in Python.

We can assign values by incrementing, decrementing, or performing any other operation and then assign the value to the variable in many different ways. The assignment operator is used to assign values to the variable. For assigning values to a variable, we have 13 assignment operators.

 

 

python-assignment-operators

All assignment operators in python are discussed below  in detail with their syntax and proper example:

1. Equal(=)

It assigns the value or value of variable directly to the variable at left hand side.

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. Caret-Equal(^=)

It performs bitwise XOR 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
8

12. 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

13. 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