Python - Augmented Assignment Operators
CBSE Class 11 - Informatics Practices
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:
Operation | Description |
---|---|
x += y | x = x + y |
x -= y | x = x - y |
x *= y | x = x * y |
x /= y | x = x / y |
x //= y | x = x // y |
x **= y | x = x ** y |
x %= y | x = x % y |
x &= y | x = x & y |
x |= y | x = x | y |
x ^= y | x = x ^ y |
x >>= y | x = x >> y |
x <<= y | x = x << y |
Q1: What is the output of the following code?
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
Unit Test Paper I - Python -2019-20
Python Programming Basics (MCQs) - YouTube Video
Python Snippets -1 (Arithmetic Operators)
Python Basics (Q & A)Python Programming Basics (MCQs) - YouTube Video
Python Snippets -1 (Arithmetic Operators)
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.