ScanSkill

= (assignment)

This = (assignment) assignment operator assigns a value to the variable(s).

Syntax

A = B

Here,

  • A: Any valid object.
  • B: Any valid object.

Note: Assignment operation = always works from right to left. The object that was referenced by the variable prior to the assignment is now dereferenced.

Examples

>>> a = 10
>>> a = 5
>>> a
5
  • Multiple assignments also follow the left-to-right as follows:
>>> a = b = c = 10
>>> a
10
>>> b
10
>>> c
10

Here, first, value 10 is assigned to the variable c, then the value of c is to b, and the value of b is assigned to c. After evaluation, all variables refer to the same object 10.

  • Multi-variable assignment
>>> a, b, c = 1, 2, 3  # <- this is a tuple
>>> a
1
>>> b
2
>>> c
3

Here, with the right-to-left rule, a tuple containing (1, 2, 3) is created, then it is iterated and its consequent values are assigned to the comma-separated list of variables on the left.

  • Swapping values
>>> a = 0
>>> b = 1
>>> a, b = b, a
>>> a
1
>>> b
0
  • New value based on existing values
>>> a = 10
>>> b = 5
>>> a, b = a + b, a * b
>>> a
15
>>> b
50

Another case:

>>> a = 10
>>> b = 5
>>> a = a + b
>>> b = a * b
>>> a
15
>>> b
75