• School Guide
  • Class 11 Syllabus
  • Class 11 Revision Notes
  • Maths Notes Class 11
  • Physics Notes Class 11
  • Chemistry Notes Class 11
  • Biology Notes Class 11
  • NCERT Solutions Class 11 Maths
  • RD Sharma Solutions Class 11
  • Math Formulas Class 11
  • Write an Admission Experience
  • Share Your Campus Experience
  • Python Tokens and Character Sets
  • Open Source and Open Data
  • Define Illegal Downloading
  • Boolean Algebra
  • How to Communicate Safely over Internet?
  • What is Licensing?
  • Impact of Technology on Society
  • Applications and Uses of Blockchain
  • What is Decomposition Computational Thinking?
  • What is Online Privacy and how is it affected?
  • Expressions in Python
  • What is Digital Rights Management? Definition, Working, Benefits
  • What is the Difference between Interactive and Script Mode in Python Programming?
  • What is Internet Trolling and How to deal with it?
  • Cloud Deployment Models
  • What is Phishing?
  • How to Browse the Internet Safely?
  • What is Cyber Bullying? Definition, Types, Effects, Laws
  • What is Plagiarism? Definition, Types, How to Avoid, Laws
  • What is a Scam?
  • GeeksforGeeks School
  • Difference between Domestic Business and International Business
  • Cyber Forensics

Augmented Assignment Operators in Python

An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write “ a = 5 “ to assign value 5 to variable ‘a’. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmetic or bitwise operator with the assignment operator. So assume if we need to add 7 to a variable “a” and assign the result back to “a”, then instead of writing normally as “ a = a + 7 “, we can use the augmented assignment operator and write the expression as “ a += 7 “. Here += has combined the functionality of arithmetic addition and assignment.

So, augmented assignment operators provide a short way to perform a binary operation and assigning results back to one of the operands. The way to write an augmented operator is just to write that binary operator and assignment operator together. In Python, we have several different augmented assignment operators like +=, -=, *=, /=, //=, **=, |=, &=, >>=, <<=, %= and ^=. Let’s see their functioning with the help of some exemplar codes:

1. Addition and Assignment (+=): This operator combines the impact of arithmetic addition and assignment. Here,

 a = a + b can be written as a += b

2. Subtraction and Assignment (-=): This operator combines the impact of subtraction and assignment.  

a = a – b can be written as a -= b

Example:  

3. Multiplication and Assignment (*=): This operator combines the functionality of multiplication and assignment.  

a = a * b can be written as a *= b

4. Division and Assignment (/=): This operator has the combined functionality of division and assignment.  

a = a / b can be written as a /= b

5. Floor Division and Assignment (//=): It performs the functioning of floor division and assignment.  

a = a // b can be written as a //= b

6. Modulo and Assignment (%=): This operator combines the impact of the modulo operator and assignment.  

a = a % b can be written as a %= b

7. Power and Assignment (**=): This operator is equivalent to the power and assignment operator together.  

a = a**b can be written as a **= b

8. Bitwise AND & Assignment (&=): This operator combines the impact of the bitwise AND operator and assignment operator. 

a = a & b can be written as a &= b

9. Bitwise OR and Assignment (|=): This operator combines the impact of Bitwise OR and assignment operator.  

a = a | b can be written as a |= b

10. Bitwise XOR and Assignment (^=): This augmented assignment operator combines the functionality of the bitwise XOR operator and assignment operator. 

a = a ^ b can be written as a ^= b

11. Bitwise Left Shift and Assignment (<<=): It puts together the functioning of the bitwise left shift operator and assignment operator.  

a = a << b can be written as a <<= b

12. Bitwise Right Shift and Assignment (>>=): It puts together the functioning of the bitwise right shift operator and assignment operator.  

a = a >> b can be written as a >>= b

Please Login to comment...

Improve your coding skills with practice.

CodeDocs Logo

  • List of languages
  • 1 Discussion
  • 2.1 Computed assignment locations
  • 3.1 C descendants
  • 4 Supporting languages
  • 6 References

Augmented assignment

Augmented assignment (or compound assignment ) is the name given to certain assignment operators in certain programming languages (especially those derived from C ). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + (1) . Similar constructions are often available for various binary operators.

In general, in languages offering this feature, most operators that can take a variable as one of their arguments and return a result of the same type have an augmented assignment equivalent that assigns the result back to the variable in place, including arithmetic operators, bitshift operators, and bitwise operators .

For example, the following statement or some variation of it can be found in many programs:

With this version, there is no excuse for a compiler failing to generate code that looks up the location of variable x just once, and modifies it in place, if of course the machine code supports such a sequence. For instance, if x is a simple variable, the machine code sequence might be something like

and the same code would be generated for both forms. But if there is a special op code, it might be

meaning "Modify Memory" by adding 1 to x, and a decent compiler would generate the same code for both forms. Some machine codes offer INC and DEC operations (to add or subtract one), others might allow constants other than one.

More generally, the form is

where the ? stands for some operator (not always + ), and there may be no special op codes to help. There is still the possibility that if x is a complicated entity the compiler will be encouraged to avoid duplication in accessing x , and of course, if x is a lengthy name, there will be less typing required. This last was the basis of the similar feature in the ALGOL compilers offered via the Burroughs B6700 systems, using the tilde symbol to stand for the variable being assigned to, so that

would become

and so forth. This is more general than just x:=~ + 1; Producing optimum code would remain the province of the compiler.

In expression-oriented programming languages such as C, assignment and augmented assignment are expressions, which have a value. This allows their use in complex expressions. However, this can produce sequences of symbols that are difficult to read or understand, and worse, a mistype can easily produce a different sequence of gibberish that although accepted by the compiler does not produce desired results. In other languages, such as Python, assignment and augmented assignment are statements, not expressions, and thus cannot be used in complex expressions. For example, the following is valid C, but not valid Python:

As with assignment, in these languages augmented assignment is a form of right-associative assignment .

Computed assignment locations

In languages such as C, C++ and Python, an augmented assignment where the assignment location includes function calls, is mandated to only call the functions once. I.e in the statement:

The function f1 is mandated to only be called once.

If a language implements augmented assignment by macro expansion to:

Then f1 is called twice.

By language

C descendants.

In C , C++, and C# , the assignment operator is = , which is augmented as follows:

Each of these is called a compound assignment operator in said languages. [1] [2] [3]

Supporting languages

The following list, though not complete or all-inclusive, lists some of the major programming languages that support augmented assignment operators.

  • Increment and decrement operators—special case of augmented assignment, by 1
  • IEEE 754 augmented arithmetic operation
  • ^ "Assignment and compound assignment operators" .
  • ^ "C# Language Specification" . Microsoft . Retrieved 17 March 2014 .

By: Wikipedia.org Edited: 2021-06-18 15:15:46 Source: Wikipedia.org

Python Essentials by Steven F. Lott

Get full access to Python Essentials and 60K+ other titles, with a free 10-day trial of O'Reilly.

There are also live events, courses curated by job role, and more.

Augmented assignment

The augmented assignment statement combines an operator with assignment. A common example is this:

This is equivalent to

When working with immutable objects (numbers, strings, and tuples) the idea of an augmented assignment is syntactic sugar. It allows us to write the updated variable just once. The statement a += 1 always creates a fresh new number object, and replaces the value of a with the new number object.

Any of the operators can be combined with assignment. The means that += , -= , *= , /= , //= , %= , **= , >>= , <<= , &= , ^= , and |= are all assignment operators. We can see obvious parallels between sums using += , and products using *= .

In the case of mutable objects, this augmented assignment can take on special ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

Don’t leave empty-handed

Get Mark Richards’s Software Architecture Patterns ebook to better understand how to design components—and how they should interact.

It’s yours, free.

Cover of Software Architecture Patterns

Check it out now on O’Reilly

Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems every day.

what is augmented assignments

  • Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • Labs The future of collective knowledge sharing
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Questions tagged [augmented-assignment]

Augmented assignment (or compound assignment) is the name given to certain assignment operators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + 1.

  • Learn more…
  • Unanswered (my tags)

Adding (concatenating) tuples to lists in Python [duplicate]

  • concatenation
  • augmented-assignment

Lee McNally's user avatar

Why there is no speed benefit of in-place multiplication when returning a numpy array?

  • performance
  • optimization

abukaj's user avatar

How do the augmented assignment operators in C behave when the signedness of the operands do not match?

  • language-lawyer

user16217248's user avatar

pytorch's augmented assignment and requires_grad

Tony Power's user avatar

python augmented assignment statements, display the results as per the output

goldie's user avatar

Augmented assignment unexpected behavior in Python

antelk's user avatar

When do the Augmented Assignment operations behave as in-place operations and when do not?

Fahim's user avatar

Adding a string to a list adds each character separately [duplicate]

Is there an equivalent of `sum()` builtin which uses augmented assignment.

  • standard-library

Is Reflected Augmented Addition (__iadd__) in Python?

Aldahunter's user avatar

Why is a destructuring augmented assignment not possible?

  • destructuring

Evan Benn's user avatar

Regex for augmented assignment operation using positive look-ahead or look-behind

Seshadri R's user avatar

Python operator precedence with augmented assignment including sequence [duplicate]

  • operator-precedence

Manngo's user avatar

Python operator precedence with augmented assignment

Why does augmented assignment behave differently when adding a string to a list [duplicate].

Njx's user avatar

Change QR code color in Real Time

  • augmented-reality
  • android-augmented-reality

Ronak Joshi's user avatar

"countOut" -- while loop entries not from the list are not counted in augmented assignment Python 3.6

  • for-in-loop

Rish's user avatar

The possibility of an assignment operator concept for an object method

Cole's user avatar

Why does the original list change?

blueFast's user avatar

UnboundLocalError when using += on list. Why is `nonlocal` needed here when directly calling __iadd__ works fine? [duplicate]

  • python-nonlocal

tahsmith's user avatar

Python inplace Boolean operator

z0r's user avatar

Augmented assignment with frozenset

  • immutability

jfsturtz's user avatar

Is this code thread-safe in python 2.7?

  • multithreading

oshaiken's user avatar

How can I make a read-only property mutable?

  • new-style-class

JuSTMOnIcAjUSTmONiCAJusTMoNICa's user avatar

Magic method for `self[key] += value`?

  • magic-methods

Bob Stein's user avatar

Python one line if-else with different operators

  • conditional-expressions

ovunctuzel's user avatar

Function parameter is reference? [duplicate]

kker neo's user avatar

Pycharm unresolved attribute reference warning

MaxPowers's user avatar

Can I use += on multiple variables on one line?

Theo Pearson-Bray's user avatar

python augmented assignment for boolean operators

nonagon's user avatar

Python ternary conditional for joining string

Luigi's user avatar

Augmented assignment in the middle of expression causes SyntaxError [duplicate]

  • syntax-error

Zach Gates's user avatar

How does Fortran handle augmented assignment of arrays?

  • variable-assignment

rayhem's user avatar

android augmented reality image targets

user3345767's user avatar

Why is it that Lua doesn't have augmented assignment? [duplicate]

Plakhoy's user avatar

python list comprehension vs +=

  • list-comprehension

hetepeperfan's user avatar

a simple program in python , I am stumped [closed]

Abhishek Dujari's user avatar

How to implement "__iadd__()" for an immutable type?

martineau's user avatar

Adding a string to a list using augmented assignment

joaquin's user avatar

"+=" causing SyntaxError in Python

jason's user avatar

Why does += behave unexpectedly on lists?

eucalculia's user avatar

  • The Overflow Blog
  • What it’s like being a professional workplace bestie (Ep. 603)
  • Featured on Meta
  • Moderation strike: Results of negotiations
  • Our Design Vision for Stack Overflow and the Stack Exchange network
  • Temporary policy: Generative AI (e.g., ChatGPT) is banned
  • Call for volunteer reviewers for an updated search experience: OverflowAI Search
  • Discussions experiment launching on NLP Collective

Related Tags

Hot network questions.

  • Fantasy book about a boy whose family lives in a sort of vacation spot for the real world
  • What is the traction conversion between T and H vs A and AA traction on automobile tires?
  • Tool to extract rings from zeolite structures
  • I'm liking to take it easy on the weekend
  • polynomials and dimensions
  • Could you find the way in this maze of rebuses?
  • Is this Linear Logic Prover Wrong?
  • Sustainable eating habits as a pilot
  • Rotation constraints overwrite inherited scale
  • Unexpected tangency
  • Why do static lambdas (C# 9) still have a target?
  • Am I considered a non-resident of California?
  • Is the liquid inside the canned chickpeas meant for consumption?
  • Applying to a tenure track job at the same school two years in a row
  • What are all the possible codes for {{A\\B}}?
  • How can I raise behavioral concerns as a candidate?
  • How to know which areas of a new song to sing softly and which areas to sing loudly?
  • Musical murder mystery
  • scantoken adds unwanted space
  • Pros and cons to round PCB design
  • Small dents on bicycle frame
  • Why do many AI bots feel the need to be know-it-alls?
  • What adhesive solution should use for walls with powdery surface?
  • Why do headphone speakers sound 'tinny' when far from the ear?

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

IMAGES

  1. PPT

    what is augmented assignments

  2. PPT

    what is augmented assignments

  3. Assignment and Augmented Assignment in Python

    what is augmented assignments

  4. Augmented Assignments and Pre/Post Increments

    what is augmented assignments

  5. Mrs Barnett First Grade

    what is augmented assignments

  6. Cosc Notes 2023

    what is augmented assignments

VIDEO

  1. AR DOCUMENTATION

  2. (9) Intro to AR Development

  3. augmentedreality #augmentedreality #shorts #fact

  4. Augmented Reality Jacket #augmentedreality #sparkarstudio #meta #areffect #ai #sparkartutorial

  5. From This... To This

  6. Locking assignments

COMMENTS

  1. Augmented assignment

    Augmented assignment(or compound assignment) is the name given to certain assignmentoperatorsin certain programming languages(especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variableas one of its arguments and then assigns the result back to the same variable.

  2. Augmented Assignment Operators in Python

    The way to write an augmented operator is just to write that binary operator and assignment operator together. In Python, we have several different augmented assignment operators like +=, -=, *=, /=, //=, **=, |=, &=, >>=, <<=, %= and ^=. Let’s see their functioning with the help of some exemplar codes: 1.

  3. Augmented assignment

    Augmented assignment (or compound assignment) is the name given to certain assignment operators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable.

  4. Python's Assignment Operator: Write Robust Assignments

    Augmented Assignments for Concatenation and Repetition. The += and *= augmented assignment operators also work with sequences, such as lists, tuples, and strings. The += operator performs augmented concatenations, while the *= operator performs augmented repetition.

  5. Augmented Assignment (Sets)

    Let’s take a deep dive into how augmented assignment actually works. You saw that many of the modifying set methods have a corresponding augmented assignment. Like we saw, intersection update (&=), and difference update (-=), and symmetric…

  6. Augmented assignment

    The augmented assignment statement combines an operator with assignment. A common example is this: a += 1 This is equivalent to a = a + 1 When working with immutable objects (numbers, strings, and tuples) the idea of an augmented assignment is syntactic sugar. It allows us to write the updated variable just once.

  7. Newest 'augmented-assignment' Questions

    Ask Question. Augmented assignment (or compound assignment) is the name given to certain assignment operators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same ...