Assignment operators

Table of contents:

Python Assignment Operators

We have already seen that the simplest way of variable assignment takes the following form:

variable = literal or expression

Table of contents:

The assignment operator =

The = operator is essential to create a new python variable, and used frequently in programming.

In the above two variables x and y have been defined. A numeric literal has been assigned to x, and an expression is assigned to y. Both are assignment statements which do not produce any output, but multiple actions have been taken internally.

When the statement x = 257 is executed,

The data-type of the variables will be reflected in the output of type().

We can now technically say that x points to the integer object 257, or x refers to the integer object 257.

When the statement y = x + 3.5 is executed,

In an assignment statement, the left hand side must be variable, and the right hand side must be a valid python expression. The statement
  aaa = bbb + 5 * xxx
is completely valid, but 1 = x is an invalid statement.

Let us find some other forms of assigning variables.

Parallel assignment

In this format, multiple variable names are kept to the left side of the = operator, while multiple values are kept to the right side of the operator.

In line 2, variables a and b are assigned the values 100 and 200 respectively. In line 5,

When two variables interchange their values, we call it swapping of variables. In the above, value of a goes to b, while value of b goes to a.

Multiple assignment

Variables x, y and z get the same integer literal 42. All of them point to the same object. Variables counter1 and counter2 are initialized to zero. Both of them point to the same object.

Assignment by equalising

Variables x and y contain the same integer literal -6. Both of them point to the same object.

Augmented assignment operators

Another type of assignment is frequently found in programming languages:

i = i + 1

This statement can never be true mathematically, but it is an important syntax in programming, where the value of variable i is increased by 1. The variable i must be defined prior to the execution of the statement.

The fourth statement counter = counter + 1 interprets like the following:
add 1 to the existing value of couner, and store the result in couner.

Augmented assignment operator +=

Now, instead of i = i + 1, the operator += can be used:

Executing counter += 1 is the same as executing counter = counter + 1 (there is an exception though).

Another example goes like this:

For every augmented assignment statements, the variable involved must be defined earlier (before the execution of the statement).

Augmented assignment operator -=

In the 2nd assignment statement a -= 5, variable a holds the literal -3 (a = a - 5 => a = 2 - 5 => a = -3).

Augmented assignment operator *= and /=

Instead of b = b * 10,
assignment statement b *= 10 is used.

Instead of myvar = myvar / 2,
assignment statement myvar /= 2 is used.

Augmented assignment operator **=

Instead of number = number ** 4,
assignment statement number **= 4 is used which is much shorter.

Augmented assignment operator //=

Instead of x = x // 5,
assignment statement x //= 5 is used.

Augmented assignment operator %=

Instead of y = y % 7,
assignment statement y %= 7 is used.

The walrus operator :=

We have used many assignment statements in our earlier examples. These statements do not produce any output. In other words, they do not return anything.

The interpreter silently assigns the integer literal 99 to the variable number, and waits for next input in the next line.
To display the assigned value we need a second input:

As IDLE window allows to omit the print, We have just entered the variable name in the second line, and the output is displayed. The above assignment in the first line is a simple assignment statement.

Variable assignment with the walrus operator takes the following form:

(variable := literal or expression)

The walrus operator := assigns the literal 99 to variable number, as well as displays the value of the variable. Unlike simple assignment statements, the walrus operator produces an output, or in a programmers's words, it returns a value.

Such a statement, containing the python walrus operator, is known as an assignment expression. It performs two tasks: assigns a value to a variable, and returns the value too. A pair of parentheses is mandatory in such expressions, without which, the interpreter will raise an error .

To implement the utility of the walrus operator :=, we reqiure python conditional branching if else or python loops using for or while. Since we are yet to learn how to use loops in python, let us try the following example.

Suppose we have to display four consecutive odd numbers by incrementing a variable next_odd, given the first one:

Now we utilize the walrus operator := to get the same output.

Of course we shall find better utility of the walrus operator while doing conditional branching and loops in python.

Next, we shall delve into operator precedence in python.

--- x ---

Want to leave a message for me?