0% found this document useful (0 votes)
0 views

Augmented Assignment Operators

Augmented Assignment Operators provide a shorthand for combining arithmetic or bitwise operations with assignment, such as using 'x += 5' instead of 'x = x + 5'. Common operators include +=, -=, *=, and /=, each performing a specific operation and assignment in one step. This approach results in shorter, cleaner, and more understandable code.

Uploaded by

kalpanapriyam213
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Augmented Assignment Operators

Augmented Assignment Operators provide a shorthand for combining arithmetic or bitwise operations with assignment, such as using 'x += 5' instead of 'x = x + 5'. Common operators include +=, -=, *=, and /=, each performing a specific operation and assignment in one step. This approach results in shorter, cleaner, and more understandable code.

Uploaded by

kalpanapriyam213
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

What Are Augmented Assignment Operators?

Augmented Assignment Operators are a shortcut to combine:

• an arithmetic or bitwise operation


• with assignment

Instead of writing:

x = x + 5

You can write:

x += 5

That’s an augmented assignment!

Common Augmented Assignment Operators


Operator Meaning Example Same as
+= Add and assign x += 2 x = x + 2
-= Subtract and assign x -= 2 x = x - 2
*= Multiply and assign x *= 2 x = x * 2
/= Divide and assign x /= 2 x = x / 2
//= Floor divide and assign x //= 2 x = x // 2
%= Modulus and assign x %= 2 x = x % 2
**= Power and assign x **= 2 x = x ** 2
&= Bitwise AND and assign x &= 2 x = x & 2
` =` Bitwise OR and assign `x
^= Bitwise XOR and assign x ^= 2 x = x ^ 2
>>= Bitwise right shift and assign x >>= 1 x = x >> 1
<<= Bitwise left shift and assign x <<= 1 x = x << 1

Simple Examples
+= Add and Assign
x = 10
x += 5 # x = x + 5
print(x) # Output: 15

-= Subtract and Assign


x = 20
x -= 4 # x = x - 4
print(x) # Output: 16

*= Multiply and Assign


x = 3
x *= 4 # x = x * 4
print(x) # Output: 12

/= Divide and Assign


x = 10
x /= 2 # x = x / 2
print(x) # Output: 5.0

**= Power and Assign


x = 2
x **= 3 # x = x ** 3
print(x) # Output: 8

Bitwise Example: &=


x = 6 # Binary: 110
x &= 3 # Binary of 3: 011 → 110 & 011 = 010 (2)
print(x) # Output: 2

Summary
Augmented assignment makes your code:

Shorter
Cleaner
Easier to understand

You might also like