Python Assignment Operators

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)Code language: Python (python)

Try it

Output:

0Code language: Python (python)

You can perform a calculation on a variable and assign the result back to it as follows:

count = 0
count = count + 1

print(count)Code language: Python (python)

Try it

Output:

1Code language: Python (python)

In 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 + 1Code language: Python (python)

In 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)Code language: Python (python)

Try it

Output:

1Code language: Python (python)

In this example, the operator += adds one to the count variable and assigns its result back to the variable:

count += 1Code language: Python (python)

It’s equivalent to the following:

count = count + 1Code language: Python (python)

Here are the common compound assignment operators in Python:

OperatorDescriptionabExampleEquivalent ToResult
+=Add and Assign52a += ba = a + b7
-=Subtract and Assign52a -= ba = a - b3
*=Multiply and Assign52a *= ba = a * b10
/=Divide and Assign52a /= ba = a / b2.5
//=Floor Divide and Assign52a //= ba = a // b2
%=Modulus and Assign52a %= ba = a % b1
**=Exponentiate and Assign52a **= ba = a ** b25

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)Code language: Python (python)

Try it

Output:

1Code language: Python (python)

Subtract 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)Code language: Python (python)

Try it

Output:

3Code language: Python (python)

Multiply 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: 20Code language: Python (python)

Try it

Output:

20

Divide 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)Code language: Python (python)

Try it

Output:

12.5Code language: Python (python)

Floor 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)Code language: Python (python)

Try it

Output:

3Code language: Python (python)

Modulus and Assign (%=) #

The modulus and assign operator ( %=) calculates the remainder and assigns it to a variable:

amount = 10
amount %= 3

print(amount)Code language: Python (python)

Try it

Output:

1Code language: Python (python)

Exponentiate 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)Code language: Python (python)

Try it

Output:

8Code language: Python (python)

Summary #

  • 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 #

Was this tutorial helpful ?