Python Assignment Operators Complete Tutorial

  • Post author:
  • Post category:Python

In this section, we will learn what Assignment operators are and how to use them in Python.

Note: we’re assuming you already familiar with operators in general.

What Are Assignment Operators in Python?

In Python, the assignment operators are those operators that we can use to assign a value to a variable.

For example, the `=` is called `assignment` operator and we use it when we want to make a variable to point to a memory space where a value is currently stored there.

List of assignment operators in Python

Here’s the list of assignment operators:

Operator Symbol
Assignment operator =
Addition and Assignment operator +=
Subtraction and Assignment operator -=
Multiplication and Assignment operator *=
Division and Assignment operator /=
Exponentiation and Assignment Operator **=
Modulus and Assignment Operator %=
Floor Division and Assignment Operator //=

Python LValues and RValues

When working with assignment operators, you should be familiar with two terminologies:

– LValues: the value we put on the left side of the assignment operators is called LValue and it should be a variable that points to a memory space. This means we can’t use a value like 10 or “string-value” as the LValue (because they don’t point to a memory space).

– RValues: the value we put on the right side of the assignment operator is called RValue. This value could be any expression that result a final value. For example, we can put a call to a function as the RValue (if that function returns a value as a result of calling it). A variable that is currently pointing to a memory space could also be used as RValue (in such case, the variable on the left side of assignment operators will point to the same memory space that the RValue is currently pointing to).

Python Assignment `=` Operator:

The assignment operator with the symbol `=` is used to store a value into a memory space and then make a variable to point to that memory space.

Note: as mentioned in the Python variable section, a variable does not have a memory space! They are just a pointer to other memory spaces that already have values in them.

So when using the assignment operator, what is happening is that the target value first will be stored in a memory location and then the target variable will point to the address of that memory. Then we can use the variable to take the value from that memory.

Assignment `=` Operator Syntax:

Variable = Value

Assignment `=` Operator Example:

age = 20

Here, the value 20 is stored in the memory and the age variable is now pointing to that memory location. So we can use the age variable to get this value whenever that was needed.

Shorthand operators (AKA Compound operators):

The compound operators are a combination of two operators!

As you’ll soon see, compound operators are a shorter way of running two separate statements in one single statement.

Python Addition and Assignment `+=` Operator:

The Python addition and assignment operator is used to:

  1. Take the value of a variable.
  2. Add it to another value.
  3. Store the result in a section of the memory and then make the first variable (the one that it took its value) to point to this new memory location.

Note: so the variable will no-longer point to the old value in the old memory space.

Addition and Assignment `+=` Operator Syntax:

Variable += Value

Addition and Assignment `+=` Operator Example:

age = 20

age += 30

print (age)

Output:

50

If we wanted to do this operation using two separate statements using + and = operators, that would be:

age = 20

res = 20 + 30

age = res

print (age)

As you can see, using the compound operators is a lot shorter and faster.

Python Subtract and Assignment `-=` Operator:

The subtract and assignment operator is used to:

  1. Take the value of a variable.
  2. Subtract that value from another value.
  3. Store the result in a section of the memory.
  4. Point the first variable (the one that is involved in the operation) to point to this new memory location.

Subtract and Assignment `-=` Operator Syntax:

Variable -= Value

Subtract and Assignment `-=` Operator Example:

age = 100

age -= 30

print(age)

Output:

70

Python Multiplication and Assignment `*=` Operator:

The multiplication and assignment operator is used to:

  1. Take the value of a variable.
  2. Multiply that value to another value.
  3. Store the result into a memory location.
  4. Make the involved variable to point to this new memory location.

Multiplication and Assignment `*=` Operator Syntax:

Variable *= Value

Multiplication and Assignment `*=` Operator Example:

salary = 30000

salary *= 2

print(salary)

Output:

60000

Python Division and Assignment `/=` Operator:

The Python division and assignment operator is used to:

  1. Take the value of a variable.
  2. Divide that value by another value.
  3. Store the result into a memory location.
  4. Make the variable to point to that new memory location.

Division and Assignment `/=` Operator Syntax:

Variable /= Value

Division and Assignment `/=` Operator Example:

salary = 30000

salary /=2

print(salary)

Output:

15000

Python Floor Division and Assignment `//=` Operator

The floor division and assignment operator is used to:

  1. Take the value of a variable.
  2. Run the floor division operation on the value of the variable by another value (the one that we put on the right side of this operator)
  3. Store the result into a memory location.
  4. Make the involved variable to point to the new memory location.

Floor Division and Assignment `//=` Operator Syntax:

Variable //= Value

Floor Division and Assignment `//=` Operator Example:

price = 52

price // = 7

print(price)

Output:

7

Python Exponent and Assignment `**=` Operator

The Exponent and Assignment operator is used to:

  1. Take the value of a variable.
  2. Exponent the value of the variable by another value (which we put on the right side of this operator).
  3. Store the result into a memory location.
  4. Make the variable to point to this new memory location.

Exponent and Assignment `**=` Operator Syntax:

Variable **= Value

Exponent and Assignment `**=` Example:

num = 8

num **=2

print(num)

Output: 64

Leave a Reply