You can learn about Operators in Python Programs with Outputs helped you to understand the language better.

Python Programming – Assignment (Augmented) Operators

Assignment operators are used to assigning values to the variables. They combine the effect of arithmetic and assignment operator. Augmented assignments can make your code more compact, concise, and Python language offers special shorthand operators that combine assignments with each of the arithmetic operations to simplify the coding. The operators are given in Table 4.3. The equivalences are shown in Figure 4.12.

Operator Symbol Form

Operation

Assign = a = b put the value of b into a
add-assign += a += b put the value of a + b into a
subtract-assign -= a -= b put the value of a-b into a
multiply-assign *_ a * = b put the value of a*b into a
divide-assign / = a / = b put the value of a/b into a
remainder-assign %= a %= b put the value of a % b into a

Example 14.
Demo of the assignment operator.

# Demo of assignment operator
a = 2
b = 10
c = 0
c = a + b
print(“Value of c is “, c)
c += a
print(“Value of c += a (add-assign) is “, c)
c *= a
print(“Value of c *= a (multiply-assign) is “, c)
c /= a
print(“Value of c /= a (divide-assign) is “, c)
c **= a
print(“Value of c **= a (exponential assign) is “, c)
c //= a
print(“Value of c //= a (floor division assign) is “, c)
c %= a
print(“Value of c %= a (remainder-assign) is “, c )RUN
Value of c is 12
Value of c += a (add-assign) is 14
Value of c *= a (multiply-assign) is 28
Value of c /= a (divide-assign) is 14.0
Value of c **= a (exponential assign) is 196.0
Value of c //= a (floor division assign) is 98.0
Value of c %= a (remainder-assign) is 0.0

For example, the expression:
j = j * 5
can be written as
j *= 5
One of the main reasons for using the arithmetic assignment operators is to avoid spelling mistakes and make code more readable. For example, the expression:
op_big_x_dimension = op_big_x_dimension
*2
can be written as
op_big_x_dimension *= 2
The second version is easier to read and write and contains fewer chances of spelling errors.

Let’s Try

Execute the following codes and write their output:

a=2 a=2 a=2
a+1 a += 1 a= a+1
print ( a ) print ( a ) print ( a )

 

Assuming the value of variable x as 10, execute the following codes and write their output:

>>> a+=2 >>> x/=2 >>> x//=2
>>> x- = 2 >>> x%=2
>>> x* = 2 >>> x**=2

Leave a Reply

Your email address will not be published. Required fields are marked *