Computer Science · Ch 5 — Getting Started with Python
Assignment Operators
Assignment Operators
An assignment operator assigns a value to — or changes the value of — the variable on its left-hand side. Besides the plain =, Python offers a family of augmented (shorthand) assignment operators that combine an arithmetic operation with assignment in a single step.
Simple assignment: =
Assigns the value of the right-side operand to the left-side operand.
>>> num1 = 2
>>> num2 = num1
>>> num2
2
>>> country = 'India'
>>> country
'India'
Augmented assignment operators
Each shorthand form x op= y means exactly the same as x = x op y — the operation is performed first, and the result is stored back in the left-side variable.
Add and assign: +=
Adds the right-side operand to the left-side operand and stores the result in the left-side operand. x += y is the same as x = x + y.
>>> num1 = 10
>>> num2 = 2
>>> num1 += num2
>>> num1
12
>>> num2
2
Because + also concatenates strings, += works on strings too:
>>> str1 = 'Hello'
>>> str2 = 'India'
>>> str1 += str2
>>> str1
'HelloIndia'
Subtract and assign: -=
Subtracts the right-side operand from the left-side operand and assigns the result to the left. x -= y is the same as x = x - y.
>>> num1 = 10
>>> num2 = 2
>>> num1 -= num2
>>> num1
8
Multiply and assign: *=
Multiplies the two operands and assigns the result to the left-side operand. x *= y is the same as x = x * y.
>>> num1 = 2
>>> num1 *= 3
>>> num1
6
Since * also repeats strings, *= does as well:
>>> a = 'India'
>>> a *= 3
>>> a
'IndiaIndiaIndia'
Divide and assign: /=
Divides the left-side operand by the right-side operand and assigns the quotient to the left. x /= y is the same as x = x / y. As with plain division, the result is a float:
>>> num1 = 6
>>> num2 = 3
>>> num1 /= num2
>>> num1
2.0
Modulus and assign: %=
Performs the modulus (remainder) operation and stores the result in the left-side operand. x %= y is the same as x = x % y.
>>> num1 = 7
>>> num2 = 3
>>> num1 %= num2
>>> num1
1
Floor-divide and assign: //=
Performs floor division and assigns the result to the left-side operand. x //= y is the same as x = x // y.
>>> num1 = 7
>>> num2 = 3
>>> num1 //= num2
>>> num1
2
Exponent and assign: **=
Performs the power calculation and assigns the result to the left-side operand. x **= y is the same as x = x ** y.
>>> num1 = 2
>>> num2 = 3
>>> num1 **= num2
>>> num1
8
Quick reference
| Operator | Shorthand for | Example (start with num1 = 10, num2 = 2) | num1 becomes |
|---|---|---|---|
= | plain assignment | num1 = num2 | 2 |
| Operator | Description | Example (Try in Lab) |
|---|---|---|
| = | Assigns value from right-side operand to left-side operand | >>> num1 = 2 >>> num2 = num1 >>> num2 2 >>> country = 'India' >>> country 'India' |
| += | It adds the value of right-side operand to the left-side operand and assigns the result to the left-side operand Note: x += y is same as x = x + y | >>> num1 = 10 >>> num2 = 2 >>> num1 += num2 >>> num1 12 >>> num2 2 >>> str1 = 'Hello' >>> str2 = 'India' >>> str1 += str2 >>> str1 'HelloIndia' |
| -= | It subtracts the value of right-side operand from the left-side operand and assigns the result to left-side operand Note: x -= y is same as x = x - y | >>> num1 = 10 >>> num2 = 2 >>> num1 -= num2 >>> num1 8 |
| *= | It multiplies the value of right-side operand with the value of left-side operand and assigns the result to left-side operand Note: x *= y is same as x = x * y | >>> num1 = 2 >>> num2 = 3 >>> num1 *= 3 >>> num1 6 >>> a = 'India' >>> a *= 3 >>> a 'IndiaIndiaIndia' |
| /= | It divides the value of left-side operand by the value of right-side operand and assigns the result to left-side operand Note: x /= y is same as x = x / y | >>> num1 = 6 >>> num2 = 3 >>> num1 /= num2 >>> num1 2.0 |
| %= | It performs modulus operation using two operands and assigns the result to left-side operand Note: x %= y is same as x = x % y | >>> num1 = 7 >>> num2 = 3 >>> num1 %= num2 >>> num1 1 |