HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

9) Operators and Booleans Lesson

Python Assignment Operator

5 min to complete · By Martin Breuss

All assignment operators are used to assign values to variables. Wait, is there more than one assignment operator? Yes, but they're all quite similar to the ones you've seen. You've used the most common assignment operator, and its symbol is a single equals sign (=).

For example, to assign x the value of 10 you type the following:

x = 10

Different Assignment Methods

You have used this assignment statement before to assign values to variables. Apart from this very common way of using it, a few other situations use the same symbol for slightly different assignments.

Chained Assignment

You can assign the same value to multiple variables in one swoop by using an assignment chain:

x = y = z = 10

This construct assigns 10 to x, y, and z. Using the chained assignment statement in Python is rare, but if you see it around, now you know what that's about.

Shorthand Assignment

Shorthand assignments, on the other hand, are a common occurrence in Python code. This is where the other assignment operators come into play. Shorthand assignments make writing code more efficient and can improve readability---at least once you know about them!

For example, think of a situation where you have a variable x and you want to add 1 to that variable:

x = 2
x = x + 1
print(x)  # 3

This works well and is perfectly fine Python code. However, there is a more concise way of writing the same code using shorthand assignment:

x = 2
x += 1
print(x)  # 3

Check out how the second line in these two code snippets is different. You don't need to write the name of the variable x a second time using the shorthand operator += like in the example above.

Both code examples shown achieve the exact same result and are equivalent. The shorthand assignment allows you to use less code to complete the task.

Shorthand Assignment Operators

Python comes with a couple of shorthand assignment operators. Some of the most common ones include the following:

Operator Meaning
+= Add the value on the right to the variable on the left
-= Subtract the value on the right from the variable on the left
*= Multiply with the variable on the right, and add to the result on the left
/= Divide by the variable on the right, and add to the result on the left

These operators are combinations of familiar arithmetic operators with the assignment operator (=). You have already used some of Python's arithmetic operators, and you'll learn more about them in the upcoming lesson.

Playground: Assignment Operator Practice

Play around and combine different operators you can think of with the assignment operator below.

x = 3
x += 1
print(x)
  • Which ones work and do what you expect them to?
  • Which ones don't?

Summary: Python Assignment Operator

  • Assignment operators are used to assign values to variables
  • Shorthand assignment is the most commonly used in Python
  • The table summarizing the assignment operators is provided in the lesson

Assignment Methods

  • Chain Assignment: A method used to assign multiple variables at one
  • Shorthand Assignment: A series of short forms for manipulating data