In programming, it is common to use assignments to modify the contents of a variable.
Remember that everything to the right of the equals sign (=
) is evaluated first, so we can say:
myVar = myVar + 5
to add 5
to myVar
.
Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.
One such operator is the +=
operator:
myVar = 10
myVar += 5
print(myVar)
This code would print 15
to the console. We call myVar += 5
an augmented assignment.
There is one such operator for every other arithmetic operator we've learned:
For subtraction: -=
myVar = 10
myVar -= 5 # short for myVar = myVar - 5
print(myVar) # Prints 5
For multiplication: *=
myVar = 3
myVar *= 5 # short for myVar = myVar * 5
print(myVar) # Prints 15
For division: /=
myVar = 20
myVar /= 4 # short for myVar = myVar / 4
print(myVar) # Prints 5
For remainder: %=
myVar = 11
myVar %= 3 # short for myVar = myVar % 3
print(myVar) # Prints 2
For exponentiation: **=
myVar = 3
myVar **= 3 # short for myVar = myVar ** 3
print(myVar) # Prints 27
For floor division: //=
myVar = 20
myVar //= 3 # short for myVar = myVar // 3
print(myVar) # Prints 6
Assignment
Follow the Coding Tutorial and let's do some augmented assignments!
Hint
Look at the examples above if you get stuck.
In this lesson, we will explore augmented assignment operators in Python. These operators are a shorthand way of performing an operation and an assignment in a single step. They are widely used in programming to make code more concise and readable. Understanding these operators is crucial for writing efficient and clean code.
Before diving into augmented assignment operators, let's revisit the concept of variable assignment. In Python, you can assign a value to a variable using the equals sign (=
). For example:
myVar = 10
To modify the value of myVar
, you might add 5 to it like this:
myVar = myVar + 5
This pattern is so common that Python provides a shorthand way to do this using augmented assignment operators.
Augmented assignment operators combine an arithmetic operation with an assignment. Here are the key operators:
+=
: Addition and assignment-=
: Subtraction and assignment*=
: Multiplication and assignment/=
: Division and assignment%=
: Modulus and assignment**=
: Exponentiation and assignment//=
: Floor division and assignmentLet's see how each of these works with examples:
+=
)myVar = 10
myVar += 5
print(myVar) # Prints 15
-=
)myVar = 10
myVar -= 5
print(myVar) # Prints 5
*=
)myVar = 3
myVar *= 5
print(myVar) # Prints 15
/=
)myVar = 20
myVar /= 4
print(myVar) # Prints 5.0
%=
)myVar = 11
myVar %= 3
print(myVar) # Prints 2
**=
)myVar = 3
myVar **= 3
print(myVar) # Prints 27
//=
)myVar = 20
myVar //= 3
print(myVar) # Prints 6
Let's look at some practical examples where augmented assignment operators can be useful:
counter = 0
for i in range(5):
counter += 1
print(counter) # Prints 5
In this example, the +=
operator is used to increment the counter in a loop.
scale_factor = 2
value = 10
value *= scale_factor
print(value) # Prints 20
Here, the *=
operator is used to scale a value by a factor.
While using augmented assignment operators, be mindful of the following:
/=
) as it can result in floating-point numbers.Augmented assignment operators can also be used with other data types like strings and lists. For example:
greeting = "Hello"
greeting += " World"
print(greeting) # Prints "Hello World"
numbers = [1, 2, 3]
numbers += [4, 5]
print(numbers) # Prints [1, 2, 3, 4, 5]
Here is a complete example demonstrating various augmented assignment operators:
# Initialize variables
a = 10
b = 5
# Addition
a += b # a = a + b
print(a) # Prints 15
# Subtraction
a -= b # a = a - b
print(a) # Prints 10
# Multiplication
a *= b # a = a * b
print(a) # Prints 50
# Division
a /= b # a = a / b
print(a) # Prints 10.0
# Modulus
a %= b # a = a % b
print(a) # Prints 0.0
# Exponentiation
a = 2
a **= 3 # a = a ** 3
print(a) # Prints 8
# Floor Division
a = 20
a //= 3 # a = a // 3
print(a) # Prints 6
When debugging code that uses augmented assignment operators, consider the following tips:
To test functions that use augmented assignment operators, write test cases that cover various scenarios:
def test_augmented_assignments():
x = 10
x += 5
assert x == 15
x -= 3
assert x == 12
x *= 2
assert x == 24
x /= 4
assert x == 6.0
x %= 5
assert x == 1.0
x **= 3
assert x == 1.0
x = 20
x //= 3
assert x == 6
test_augmented_assignments()
print("All tests passed!")
When working with augmented assignment operators, keep these strategies in mind:
In this lesson, we explored augmented assignment operators in Python. These operators provide a concise way to perform arithmetic operations and assignments in a single step. By mastering these operators, you can write more efficient and readable code. Practice using them in various scenarios to become proficient.
For further reading and practice, check out the following resources: