🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Assignment Operators

AI & DATA SCIENCE // assignment-operators

Assignment operators (=, +=, -=, *=, and more) bind a value to a variable name, with the compound forms combining an operation and an assignment in one step.

Syntax

x = 5
x += 1
x *= 2
x, y = 1, 2

Deep Dive Course

The plain = operator binds a name to a value — technically to an object reference, since Python variables are labels rather than fixed memory slots. Compound assignment operators like += combine an arithmetic operation with the assignment, so x += 1 is roughly shorthand for x = x + 1 — though for mutable objects like lists, += can mutate in place rather than create a new object, a subtle but important distinction. Python also supports multiple and chained assignment, like unpacking two values at once or assigning the same value to several names in one statement.

1Understanding Assignment Operators

The plain = operator binds a name to a value — technically to an object reference, since Python variables are labels rather than fixed memory slots. Compound assignment operators like += combine an arithmetic operation with the assignment, so x += 1 is roughly shorthand for x = x + 1 — though for mutable objects like lists, += can mutate in place rather than create a new object, a subtle but important distinction. Python also supports multiple and chained assignment, like unpacking two values at once or assigning the same value to several names in one statement.

💡

For a list, x += [item] mutates the list in place, while x = x + [item] always creates a brand-new list — this matters if other variables also reference the original list.

editor.html
count = 0
count += 1
count += 1
print(count)
localhost:3000

2Practical Example

Here is a real-world application of Assignment Operators showing how it is used in production Python code.

editor.html
original = [1, 2]
alias = original
alias += [3]
print(original)
print(alias is original)
localhost:3000

3Best Practices

Follow these guidelines when working with Assignment Operators:

1. Use compound operators like += for readability when updating a variable based on its own value

2. Be aware that += on a list mutates in place, unlike += on an int, str, or tuple, which always creates a new object

3. Use tuple unpacking for swaps instead of a manual temporary variable

⚠️

Tip: For a list, x += [item] mutates the list in place, while x = x + [item] always creates a brand-new list — this matters if other variables also reference the original list.

editor.html
count = 0
count += 1
count += 1
print(count)
localhost:3000

Examples

Example 01Basic Usage
count = 0
count += 1
count += 1
print(count)
Example 02Advanced Example
original = [1, 2]
alias = original
alias += [3]
print(original)
print(alias is original)

Best Practices

  • Use compound operators like += for readability when updating a variable based on its own value
  • Be aware that += on a list mutates in place, unlike += on an int, str, or tuple, which always creates a new object
  • Use tuple unpacking for swaps instead of a manual temporary variable

Interview Question

Why does using += on a list change the original list it points to, while reassigning with `alias = alias + [3]` would not?

Hint: Think about in-place mutation versus creating a new object.

For a list, += calls the list's __iadd__ method, which extends the existing list object in place rather than creating a new one — so any other variable referencing that same list, like the original, sees the change too. Reassigning with `alias = alias + [3]` instead builds a brand-new list from the concatenation and rebinds only the alias name to it, leaving the original list untouched.

Exercises

MediumPractice using Assignment Operators in a real scenario.
View Solution
count = 0
count += 1
count += 1
print(count)

Frequently Asked Questions

Why does using += on a list change the original list it points to, while reassigning with `alias = alias + [3]` would not?

For a list, += calls the list's __iadd__ method, which extends the existing list object in place rather than creating a new one — so any other variable referencing that same list, like the original, sees the change too. Reassigning with `alias = alias + [3]` instead builds a brand-new list from the concatenation and rebinds only the alias name to it, leaving the original list untouched.

Related Functions

arithmetic-operatorsliststuples