Assignment Operators
These operators are useful when assigning values
to variables:
Operators Function
assigns the value of the right operand to the left operand
|
|
|
|
|
adds the value of the right and left operand and assigns the
|
+= add and
|
total to the
left operand
|
|
|
-= subtract
|
deducts the value of the right operand from the value of the
|
and
|
left operand and assigns the new value to the left operand
|
|
|
*= multiply
|
multiplies the left and right operand and assigns the product
|
and
|
to the left
operand
|
|
|
/= divide
|
divides the left operand with the value of the right operand
|
and
|
and assigns the
quotient to the left operand
|
|
|
**=
|
performs
exponential operation on the left operand and
|
exponent
|
assigns the
result to the left operand
|
|
|
//= floor
|
performs floor division on the left operand and assigns the
|
division and
|
result to the
left operand
|
|
|
= Operator
You
have seen this operator at work in previous chapters when you have assigned
different values to variables. Examples:
a = c
a = b + c
a = 8
a = 8 + 6
s = “I love Python.”
+= add and
The
‘add and’ (+=) operator is simply another way to express x = x + a so that
you’ll end up with the statement x += a.
The
‘subtract and’ (-=) operator is equivalent to the expression x = x – a and is
expressed with the statement x-=a
*= multiply and
The
‘multiply and’ (*=) operator is the equivalent of the statement x = x * a and
is expressed with x*=a.
/= divide and
The
‘divide and’ (/=) operator is like saying x = x/a and is expressed with the
statement x/=a.
%= modulus and
The
‘modulus and’ (%=) operator is another way to say x = x % a where you’ll end up
instead with the expression x%=a.
//= floor division and
The ‘floor division and’ is equivalent to the
expression x = x//a and takes the form x//=a.
Comments
Post a Comment
If you have any query, please let us know