title | description |
---|---|
Chapter 2B: Assignment Operators |
Bonus chapter on augmented assignment operators and unary operators |
We've learned how to define a variable with the assignment operator (=
), and now we'll look at a few closely related operators - the augmented assignment operators.
We'll also learn about the unary arithmetic operators.
In this chapter you will:
- Learn what the augmented assignment operators and unary arithmetic operators are, and how they're used,
- Practise using the augmented assignment operators and unary arithmetic operators.
In the previous chapter we used the basic assignment operator (=
) to define and overwrite variables.
We also saw how variables can be used in calculations:
>>> my_first_number = 2
>>> my_second_number = my_first_number + 5
>>> my_second_number
7
>>> my_second_number = 2
>>> my_second_number
2
When using the assignment operator, it's actually valid for the right hand side of the assignment to reference the variable that's being assigned to! For example:
>>> my_number = 2
>>> my_number = my_number + 1
>>> my_number
3
my_number = my_number + 1
means that my_number
is assigned the current value of my_number
(i.e. 2) plus 1.
Incrementing a variable like this is pretty common, so Python includes an operator that provides a shortcut:
>>> my_number = 5
>>> my_number += 6
>>> my_number
11
+=
is known as an augmented assignment operator, because it's actually a combination of two operations:
- Adding a number to the variable's current value (
+
), - Assigning the updated value back to the variable (
=
).
Augmented assignment operators are sometimes called compound assignment operators.
You can also use +=
to increment a variable by the value of a different variable:
>>> my_first_number = 7
>>> my_second_number = 8
>>> my_first_number += my_second_number
>>> my_first_number
15
+=
is perhaps the most commonly used augmented assignment operator, but there's actually quite a few others.
In fact, each of the arithmetic operators we covered in chapter 2 has a corresponding augmented assignment operator:
Operator | Usage | Alternative |
---|---|---|
+= | my_number += 3 | my_number = my_number + 3 |
-= | my_number -= 3 | my_number = my_number - 3 |
*= | my_number *= 3 | my_number = my_number * 3 |
/= | my_number /= 3 | my_number = my_number / 3 |
%= | my_number %= 3 | my_number = my_number % 3 |
//= | my_number //= 3 | my_number = my_number // 3 |
**= | my_number **= 3 | my_number = my_number ** 3 |
They each work in a similar way to +=
.
For example, my_number **= 3
means that my_number
is assigned the current value of my_number
raised to the power of 3.
>>> my_number = 7
>>> my_number
7
>>> my_number += 4
>>> my_number
11
>>> my_number -= 3
>>> my_number
8
>>> my_number *= 4
>>> my_number
32
>>> my_number /= 2
>>> my_number
16.0
>>> my_number %= 9
>>> my_number
7.0
>>> my_number //= 2
>>> my_number
3.0
>>> my_number **= 3
>>> my_number
27.0
In the previous chapter we thought of variables as storage boxes, and each variable's value as the item in its box. We can expand this analogy to describe how augmented assignment operators work:
Variables | Storage boxes |
---|---|
my_variable = my_variable + 1 | Take the item out its box, update the item, and put the item back in its box |
my_variable += 1 | Update the item while it's in its box |
The effect is exactly the same either way. The
+=
is just a prettier shorthand for the same instruction.
Throughout the previous chapters we've seen how +
and -
can be used as arithmetic operators for addition and subtraction:
>>> 7 + 2
9
>>> 7 - 2
5
When using +
and -
to add or subtract, the operator is surround by two operands (e.g. 1 + 2
).
However, +
and -
can also be used as a prefix for a single operand (e.g. +1
or -1
).
When +
and -
are used like this, they're known as the unary arithmetic operators:
Name | Usage |
---|---|
Addition operator | 3 + 4 |
Subtraction operator | 7 - 13 |
Unary plus operator | +2 |
Unary minus operator | -2 |
As you might expect, putting -
before an expression changes its sign:
>>> my_number = -3
>>> my_number
-3
>>> -my_number
3
>>> -(2 + 3)
-5
Putting +
before an expression doesn't change its sign. It is almost always true that the unary +
operator has no impact, i.e. x
is equal to +x
. There are certain niche scenarios with other data types where this is not strictly true, but it mainly exists for symmetry: you may sometimes find it helps readability to treat positive and negative numbers the same.
>>> my_first_number = +1
>>> my_second_number = -1
>>> my_first_number
1
>>> my_second_number
-1
This table lists the arithmetic operators that we've seen so far from highest precedence to lowest. Operators on the same row have the same precedence.
Operator | Name |
---|---|
** | Exponentiation |
+my_number, -my_number | Unary plus, unary minus |
*, /, //, % | Multiplication, division, floor division, modulus |
+, - | Addition, subtraction |
The only arithmetic operator with a higher precedence than the unary arithmetic operators is the exponentiation operator (**
). This can lead to surprising results:
>>> -2 ** 2
-4
In this example, -2 ** 2
is equivalent to -(22), which is equal to -4.
In situations like this, it's helpful to use brackets for clarification:
>>> -(2 ** 2)
-4
>>> (-2) ** 2
4
If you'd like to read some alternate explanations, or see some more examples, then you might find these resources helpful:
- Real Python
- This site has a tutorial on expressions that covers the augmented assignment operators.
- OverIQ
- This site has a tutorial on operators that covers the augmented assignment operators and unary arithmetic operators.
We've run through the general concepts, and now we'll get some hands-on experience.
It can be tempting to jump right into running each exercise in the REPL, but it's best to try and predict the answers first. That way, you'll have a clearer idea about which concepts you find more or less intuitive.
You can check your answers for each exercise at the end of this chapter.
>>> my_number = 4
>>> my_number
<answer 1>
>>> my_number += 3
>>> my_number
<answer 2>
>>> my_number += 5
>>> my_number
<answer 3>
>>> my_number = 15
>>> my_number
<answer 1>
>>> my_number -= 6
>>> my_number
<answer 2>
>>> my_number -= 2
>>> my_number
<answer 3>
>>> my_number = 2
>>> my_number
<answer 1>
>>> my_number *= 3
>>> my_number
<answer 2>
>>> my_number *= 5
>>> my_number
<answer 3>
>>> my_number = 100
>>> my_number
<answer 1>
>>> my_number /= 4
>>> my_number
<answer 2>
>>> my_number /= 5
>>> my_number
<answer 3>
>>> my_number = 33
>>> my_number
<answer 1>
>>> my_number %= 12
>>> my_number
<answer 2>
>>> my_number %= 7
>>> my_number
<answer 3>
>>> my_number = 17
>>> my_number
<answer 1>
>>> my_number //= 2
>>> my_number
<answer 2>
>>> my_number //= 3
>>> my_number
<answer 3>
>>> my_number = 2
>>> my_number
<answer 1>
>>> my_number **= 3
>>> my_number
<answer 2>
>>> my_number **= 2
>>> my_number
<answer 3>
>>> my_first_number = -7
>>> my_first_number
<answer 1>
>>> my_second_number = +9
>>> my_second_number
<answer 2>
>>> my_first_number * my_second_number
<answer 3>
>>> (+my_first_number) * my_second_number
<answer 4>
>>> my_first_number * (-my_second_number)
<answer 5>
>>> -(my_first_number * my_second_number)
<answer 6>
There's a few issues that people can run into when using augmented assignment operators and unary arithmetic operators in Python. We've listed some of the most common ones here.
For each troubleshooting exercise, try and figure out what went wrong and how it can be fixed.
You can check your answers for each exercise at the end of this chapter.
Why is an error being printed?
>>> 5 += 7
SyntaxError: cannot assign to literal
Why is an error being printed?
>>> my_number = 9
>>> my_number * = 6
SyntaxError: invalid syntax
Why is an error being printed?
>>> my_number = 12
>>> my_number =/ 3
SyntaxError: invalid syntax
Why do my_first_number
and my_second_number
have different values?
>>> my_first_number = 7
>>> my_first_number -= 3
>>> my_first_number
4
>>> my_second_number = 7
>>> my_second_number =- 3
>>> my_second_number
-3
Why do these expressions have different values?
>>> -3 ** 2
-9
>>> (-3) ** 2
9
Why is an error being printed?
>>> my_number *= 2
NameError: name 'my_number' is not defined
We've reached the end of assignment operators, and at this point you should know:
- What the augmented assignment operators and unary arithmetic operators are,
- How to use augmented assignment operators to simplify updating variables,
- How to use arithmetic operators to change the sign of an expression.
Next up is Chapter 3: Data Types.
- 4
- 7
- 12
- 15
- 9
- 7
- 2
- 6
- 30
- 100
- 25.0
- 5.0
- 33
- 9
- 2
- 17
- 8
- 2
- 2
- 8
- 64
- -7
- 9
- -63
- -63
- 63
- 63
The +=
augmented assignment operator is a combination of addition and assignment to a variable.
In particular, 5 += 7
is equivalent to 5 = 5 + 7
.
We're trying to assign to a value (i.e. 5) instead of a variable, which isn't valid.
*=
is an augmented assignment operator.
The *
and =
have to be used in this order, without any characters in between.
/=
is an augmented assignment operator.
=/
looks very similar to /=
(the augmented assignment operator) but the operators are in the opposite order, which isn't valid.
-=
is an augmented assignment operator.
my_first_number -= 3
means that my_first_number
is assigned the current value of my_first_number
(i.e. 7) minus 3.
=-
looks very similar to -=
(the augmented assignment operator), but it's handled differently.
Python evaluates =-
by separating it into an assignment operator (=
) and a unary arithmetic operator (-
).
In this case, my_second_number =- 3
is evaluated as my_second_number =(- 3)
.
The unary arithmetic operators (+
and -
) have lower precedence than the exponentiation operator (**
).
In the first expression, 3 ** 2
is calculated first (evaluating to 9), and then -
flips the sign (evaluating to -9).
In the second expression, -3 is squared, evaluating to 9.
Augmented assignment operators can only be used on variables that have already been defined.
my_number *= 2
means that my_number
is assigned the current value of my_number
multiplied by 2.
However, my_number
hasn't been defined, so it doesn't currently have a value, making the command invalid.