Python. Assignment operator. Assignment Forms. Examples. Positional assignment of tuples, lists




Assignment operator. Assignment Forms. Examples. Positional assignment of tuples, lists


Contents


Search other websites:

1. Purpose of the assignment operator

The assignment operator is designed to associate names with values and to change the attributes or elements of variable objects. The assignment operator associates a variable with an object and a set (list) of objects. The assignment operator is denoted by ‘=’.

 

2. What are the forms of assignment instructions? Examples

In Python, the following forms (varieties) of assignment instructions exist:

  • ordinary (canonical) form. This form binds one variable (one data item) to one object;
  • positional assignment of tuples. In this case, in the left part of the assignment there are several variables (data elements) that correspond to the same number of objects in the right part. Values (objects) in the right part are assigned to names (variables) in the left part, which are located in the same positions;
  • positional assignment of lists. Works on the principle of positional assignment of tuples, but implemented for lists;
  • positional assignment of sequences. In this case, the sequence is to the right of the assignment sign. This sequence is divided into sub-elements, each of which is assigned to the corresponding name, indicated in the left part of the assignment operator;
  • assignment using the unpack operation *;
  • assignment a single object to a name group;
  • combined assignment. In this case, the following operators are used: +=, -=, *=, /=, //=,%=, &=, |=, =, >>=, <<=, **=.

 

3. The assignment of a single objects. General form. Examples

If an assignment operator is used for single objects, then its general form is:

name = value

here

  • name – name (variable) to be associated with value;
  • value – the value assigned to the name, which can be an expression, a single value or a list.

It is also allowed to perform multiple assignments to single objects in one line. In this case, the assignment operations are separated by the symbol ‘;’.

Examples.

>>> a=5 # canonical form
>>> b=7; c=8; d=9 # canonical form, separation by sign ';'
>>> b
7
>>>a
5
>>>
>>> T = [1,2,4] # assignment of the list
>>> T
[1, 2, 4]
>>>

In the above example, the variable a is associated with object 5. Also, the variables b, c, d get the values 7, 8, 9. The name T is associated with the list [1, 2, 4].

 

4. Positional assignment of tuples and lists. General form. The principle of performing positional assignment

In the simplest case, the general form of a positional assignment operator for tuples is:

name1, name2, ..., nameN = value1, value2, ..., valueN

here

  • name1, name2, nameN – names (variables) that need to be associated with objects (values) value1, value2, valueN;
  • value1, value2, valueN – values (objects) that are associated with the names name1, name2, nameN.

When assigning multiple objects, the order of assignment is formed from left to right. Names that are associated with a list of objects are recursively revised from left to right. Each name on the left side of the assignment statement corresponds to an object (value) on the right side of the assignment statement, which has the same position.

After assigning a value to the first name, this name already has a new value when assigning a value to the second name. It is important. The following examples will demonstrate the feature of the assignment operation for several objects.

 

5. Examples of using the assignment operator for multiple names

Example 1. The following code demonstrates assigning values to several objects.

>>> x=[0,1,2,3]
>>> i=0
>>> i,x[i]=2,6
>>> x
[0, 1, 6, 3]
>>>

In the above example, when assigning, the string

>>> i,x[i]=2,6

using a recursive call is sequentially formed in two lines

i=2
x[i]=6 # x[2]=6

therefore, as a result, the list-object x will be

[0, 1, 6, 3]

 

6. Positional assignment of sequences of characters. Example

The assignment operator can be used to assign sequences of characters. Among several names, the symbol on the right side is assigned to a name whose position on the left side coincides with the position of this symbol.

>>> x,y,z,w = 'abcd'
>>> x
'a'
>>> y
'b'
>>> z
'c'
>>> w
'd'
>>>


 

7. Examples of positional assignment of tuples to exchange their values

Example 1.

In this example, the two tuple values are exchanged:

>>> a,b = 5,7 # positional assignment of tuples
>>> a,b
(5, 7)
>>> a,b = b,a # exchange of values
>>> a,b
(7, 5)
>>>

In this example, the tuple assignment string

>>> a,b = b,a

works as follows:

  • first, a temporary tuple is created that stores the original values a, b to the right of the assignment operation. In this time tuple, the original values are a’=5, b’=7;
  • name a is assigned the value b’ of the time tuple. That is, a = b’ = 7;
  • name b is assigned the value a’ from the temporary tuple. That is, b = a’ = 5.

So, to exchange values in Python, you do not need to use temporary variables to store values.

Example 2.

>>> a,b,c,d = (1,2,3,4) # value assignment to names
>>> (a,b,c,d)
(1, 2, 3, 4)
>>> a,b,c,d = d,c,b,a # a = d; b = c; c = b; d = a
>>> a,b,c,d
(4, 3, 2, 1)

In this example, in the string

>>> a,b,c,d = d,c,b,a

the interpreter performs the following sequence of actions:

  • a temporary tuple is created in which the names have the following values: a = 1, b = 2, c = 3, d = 4;
  • the name a gets the value of d from the time tuple: a = 4;
  • the name b gets the value of c from the time tuple: b = 3;
  • the name c gets the value b from the temporary tuple. Thus c = 2;
  • the name d is the value of a from the temporary tuple. After all, d = 1.

 

8. Positional assignment of lists. Example

Variables can be formed to lists. The assignment operation in the lists may be as follows:

>>> [X,Y,Z] = [5,10,15] # positional assignment of lists
>>> [X,Y,Z]
[5, 10, 15]
>>> X
5
>>> Y,Z
(10, 15)
>>> [Y,Z] # submit as a list
[10, 15]
>>> (X,Y,Z) # submit as a tuple
(5, 10, 15)
>>>

 

9. Example of assignment between tuples and lists

In Python, lists and tuples are generalized. This means that it is possible to assign a list of variables to a tuple and, conversely, a tuple of a submenu list. In any case, the interpreter associates the elements of the sequence from the right with the variables in the sequence from left, according to their positions in the direction from left to right:

>>> [x, y, z] = (5, 10, 15) # a tuple of values assigned to a variable list
>>> x,y,z
(5, 10, 15)
>>> (i,j,k) = [100, 200, 300] # a list of values assigned to a tuple
>>> i,j,k
(100, 200, 300)
>>> (a,b,c,d) = "INFO" # character string assigned to a tuple
>>> a,b,c,d
('I', 'N', 'F', 'O')
>>> a,d
('I', 'O')
>>>

 

10. What restrictions are set when assigning sequences?

When assigning sequences, it is important that both sequences have the same number of items. If the number of elements in the sequences is different, the interpreter will display an error message.

For example.

>>> text = "ABCD"
>>> x,y,z = text # the number of items does not match
Traceback (most recent call last):
File "<pyshell#113>", line 1, in <module>
x,y,z = text
ValueError: too many values to unpack (expected 3)
>>>

If you need to assign a different number of elements, then in Python there is an opportunity to get a slice. More details about slices are described in another topic.

 


Related topics