Tuesday 24 December 2019

CBSE Class 11 - Informatics Practices - Python - Augmented Assignment Operators (#eduvictors)(#cbseclass11Python)

Python - Augmented Assignment Operators

CBSE Class 11 - Informatics Practices

CBSE Class 11 - Informatics Practices - Python - Augmented Assignment Operators (#eduvictors)(#cbseclass11Python)


Python provides augmented assignment operators, which combine the impact of an arithmetic operator with an assignment operator.

e.g. If you want to add value of b to a and then assign the result to a i.e.  a = a + b can be written as
a += b

Various augmented operators are:

OperationDescription
x += yx = x + y
x -= yx = x - y
x *= yx = x * y
x /= yx = x / y
x //= yx = x // y
x **= yx = x ** y
x %= yx = x % y
x &= yx = x & y
x |= yx = x | y
x ^= yx = x ^ y
x >>= yx = x >> y
x <<= yx = x << y


Q1: What is the output of the following code?
a, b = 10, 2
a += b
print(a)
a += b
print(a)

Answer:
12
14


Q2: What is the output of the following code?
x = 10
x -= 6
print(x)
x -= 6
print(x)

Answer:
4
-2


Q3: What is the output of the following code?
x, y = 2, 3
x **=y
print(x)
x **= y
print(x)

Answer:
8
512


Q4: Does x += y violate mutability?

Answer: No. x += y creates a new object x storing result of x+y.


☞See also:
 Informatics Syllabus (New) - 2019-20
 Python Basics (Q & A)
 Python Basics (Q & A) Part-2
 Python Basics - Variables (Q & A)
 Python Basics - Literals (Q & A)

No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.