Assignment Operators in Python
Assignment Operators in
Python
Assignment operators : Assignment operators are used to assign the
value to the variable.
|
Operators |
Description |
Syntax |
|
= |
Is used to
assign value to the variable. |
a = 5 |
|
+= |
Add left
operand to right operand and assign value to left operand. |
b += a |
|
-= |
Subtract
right operand from left operand and assign value to left operand. |
b -= a |
|
*= |
Multiply left
operands with right operands and assign value to left operand. |
b *= a |
|
/= |
Divide left
operand by right operand and assign value to left operand. |
b /= a |
|
**= |
Calculate
power of left operand to right operand and assign value to left operand. |
b **= a |
|
//= |
It perform
floor division as it divide left operand by right operand , it return the
value of quotient and assign result to left operand. |
b //= a |
|
%= |
Divide left
operand by right operand and return the remainder and it assign to left
operand. |
b %= a |
Example :
a=2
b=1
b += a;
print("value of b:
",b)
b -= a;
print("value of b:
",b)
b *= a;
print("value of b:
",b)
b /= a;
print("value of b:
",b)
a=2
b=15
b //= a; # the // use for
the quotient
print("value of b:
",b)
b %= a; #the % operator use
for the remainder
print("value of b:
",b)
b=2
b **= a; #the ** use for the power
print("value of b:
",b)
Output :
value of b: 3
value of b: 1
value of b: 2
value of b: 1.0
value of b: 7
value of b: 1
value of b: 4
conclusion : Here we see about the how we use assignment operators in python.

Great Information
ReplyDelete