Summary: in this tutorial, you’ll learn how to use the Python assignment operators to assign values to variables.
Assignment operator #
In Python, you use the assignment operator to assign a value to a variable. For example, the following use the assignment operator (=) to assign number 0 to the variable count:
count = 0
print(count)Output:
0You can perform a calculation on a variable and assign the result back to it as follows:
count = 0
count = count + 1
print(count)Output:
1In this example, we assign 0 to the count variable and then increment its value by one and assign it back to the count variable.
Note that the = in the following expression is an assignment, not equal comparison:
count = count + 1In Python, the equal operator that compares two values is ==. You’ll learn it in the comparison operators tutorial.
Compound Assignment Operators #
In programming, we often want to express our ideas concisely. For example, you can increment the count variable and assign its result back to the variable in one step using the compound assign operator +=:
count = 0
count += 1
print(count)Output:
1In this example, the operator += adds one to the count variable and assigns its result back to the variable:
count += 1It’s equivalent to the following:
count = count + 1Here are the common compound assignment operators in Python:
| Operator | Description | a | b | Example | Equivalent To | Result |
|---|---|---|---|---|---|---|
+= | Add and Assign | 5 | 2 | a += b | a = a + b | 7 |
-= | Subtract and Assign | 5 | 2 | a -= b | a = a - b | 3 |
*= | Multiply and Assign | 5 | 2 | a *= b | a = a * b | 10 |
/= | Divide and Assign | 5 | 2 | a /= b | a = a / b | 2.5 |
//= | Floor Divide and Assign | 5 | 2 | a //= b | a = a // b | 2 |
%= | Modulus and Assign | 5 | 2 | a %= b | a = a % b | 1 |
**= | Exponentiate and Assign | 5 | 2 | a **= b | a = a ** b | 25 |
Add and Assign (+=) #
The add and assign operator (+= ) allows you to add a value to a variable and assign the result back to it. For example:
count = 0
count += 1 # Same as: count = count + 1
print(count)Output:
1Subtract and Assign (-=) #
The subtract and assign operator (-=) allows you to subtract a value from a variable and assigns the result back to the variable:
quantity = 5
quantity -= 2 # Same as: quantity = quantity- 2
print(quantity)Output:
3Multiply and Assign (*=) #
The multiply and assign operator ( *= ) operator allows you to multiply a number and assigns the result back to the variable:
a = 10
a *= 2 # Same as: a = a * 2
print(a) # Output: 20Output:
20Divide and Assign (/=) #
The divide and assign operator ( /= ) allows you to divide and assign the result:
amount = 25
amount /= 2 # same as amount = amount / 2
print(amount)Output:
12.5Floor Divide and Assign (//=) #
The floor divide and assign operator ( //= ) allows you to perform integer division and assign a value back:
amount = 10
amount //= 3 # Same as: amount = amount // 3
print(amount)Output:
3Modulus and Assign (%=) #
The modulus and assign operator ( %=) calculates the remainder and assigns it to a variable:
amount = 10
amount %= 3
print(amount)Output:
1Exponentiate and Assign (**=) #
The exponentiate and assign operator (**= ) allows you to raise a number to a power and assign the result back:
a = 2
a **= 3 # Same as: a = a ** 3
print(a)Output:
8Summary #
- Use the assignment operator (
=) to assign a value to a variable. - Use compound assignment operators to calculate and assign the result to a variable in one step.
Quiz #
Assignment Operators
Thank you for your feedback!