0% found this document useful (0 votes)
3 views2 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

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
We take content rights seriously. If you suspect this is your content, claim it here.
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy