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)

Try it

Output:

0

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

count = 0
count = count + 1

print(count)

Try it

Output:

1

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 + 1

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)

Try it

Output:

1

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

count += 1

It’s equivalent to the following:

count = count + 1

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)

Try it

Output:

1

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)

Try it

Output:

3

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: 20

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)

Try it

Output:

12.5

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)

Try it

Output:

3

Modulus and Assign (%=) #

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

amount = 10
amount %= 3

print(amount)

Try it

Output:

1

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)

Try it

Output:

8

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 #

Quiz

Assignment Operators

10 questions

To help you understand how to use Python assignment operators to assign values to variables.

Was this helpful?