from Python Basic to advanced programming , Basically Python The introductory knowledge has been learned , In order to learn more Python Entry to transition to Python Advanced direction （ Like reptiles / back-end / Data analysis, etc ） Novices consolidate their basic knowledge , Made a special issue about Python Novice entry on the content of assignment statements .

stay Python among , There are many ways to assign values , For example, variables , tuple , The assignment of lists and so on is different , So if you're ambiguous about the assignment , It is conceivable that the later advanced content , It's hard for you to write code alone .

No more nonsense , Learn from time to time , Change what you have, and encourage what you don't have .

<> one , Common assignment statements

Let's see Python What are the commonly used assignment statements in ：

Method interpretation
a = 10 basic form
a,b = 10,20 Tuple assignment
[a,b] = [10,20] List assignment
a,b = “AB” Sequence assignment
a,b = [10,20] Sequence assignment
a,*b = “hello” Extended sequence unpacking
a = b = c =10 Multi-objective Assignment
a+=1 Enhanced assignment

Sequence is a general term for ordered sets , stay Python in , character string , list , tuple , Byte array , buffer ,Xrange Objects belong to different sequence types , in other words （10,20） It can be said that 1 Tuples , It can also be said that 1 Sequence .

give an example 1： utilize 1 One tuple assigns a value to another tuple
a,b = 10,20 print(a) print(b)
Operation results ：
10 20
be careful ： The number of elements on both sides of the equal sign should be equal , If it is a,b=10 perhaps a = 10,20 , Then an error will be reported .

give an example 2： Assign a value to another sequence using a sequence
a,b = [20,30] print(a) print(b)
Operation results ：
20 30
a,b The default is a tuple ,[20,30] Is a list , Different sequence types can also be assigned in this way .

<> two , Advanced assignment statement

What is an advanced assignment statement ? That is, the conventional assignment method will report an error , You have to do something more advanced . For example, to put characters "SPAM" Assign to a and b, The general assignment is like this ：
s = "SPAM" a,b = s print(a,b )
Then an error will be reported after running , There are more string elements than tuples （a,b） The elements inside , Direct copy will report an error .

<>1. Tips 1

In this case , We need to use the slice operation for assignment ：
s = "SPAM" a,b = s[:3],s[2:] print(a,b )
Operation results ：
SPA AM
<>2. Tips 2

Except by slicing , We can give it in other ways a and b assignment ：
s = "SPAM" (a,b),c = s[:2],s[2:] print(a,b,c )
Operation results ：
S P AM
This operation is equivalent to a large tuple （（a,b）,c） There is a small tuple in it （a,b）, Then yes a,b Assign values .

<>3. Tips 3

give an example ： List with assignment statement [1,2,3,4,5] Make each reduction 1 Select an element and output the list .
l = [1,2,3,4,5] while l : l = l[1:] print(l)
Operation results ：
[2, 3, 4, 5] [3, 4, 5] [4, 5]  []
<>4. case

There is an existing 2D list [[[1,2,3],[4,5,6],[7,8,9]] , How to use the method of sequence assignment to output each element in the list ? The method is as follows ：
s = [[1,2,3],[4,5,6],[7,8,9]] for (a,b,c) in s: print(a,b,c)
Operation results ：
1 2 3 4 5 6 7 8 9
（a,b,c） and s Are different types of sequence types , The method of sequence assignment can also be realized .

<> three , Extended sequence unpacking

In the previous common assignment statement table, we talked about the extended sequence unpacking operation ：

Mode interpretation
a,*b = “hello” Extended sequence unpacking
So how should it be used ? First of all, we should know *b What's the use ：
*b: Get the remaining data after allocation , Encapsulated into a list for output
give an example ：
a,*b = "abcdefg" print(a) print(b)
Operation results ：
a ['b', 'c', 'd', 'e', 'f', 'g']
As can be seen from this example ,a Assigned by the first element , I gave the rest b, And the remaining elements are encapsulated into a list .

<>1. Tips 1

Now that we've talked about it
a,*b = " character string "
So what if we add one more element to assign a value ? What happens ?
a,*b,c = "abcdefg" print(a) print(b) print(c)
Operation results ：
a ['b', 'c', 'd', 'e', 'f'] g
What you can see is , Really c An object is left and assigned , This method can be used flexibly .

<>2. Tips 2

If we change positions ：
*b,a = "abcdefg" print(a,b)
Operation results ：
g ['a', 'b', 'c', 'd', 'e', 'f']
It can be seen that , The last element of the string g Or is it assigned to a, So it's OK to switch positions , At this time a It will be taken from the back .

<>3. Tips 3

If we only give 3 Elements , But there are 4 Elements to assign , that * b Can you get the value ?
a,c,*b,d = "abc" print(a,b,c,d)
Operation results ：
a [] b c
result b yes 1 Empty list , Because the string has only three characters , Not enough b, alike , No matter how you exchange * b Location of , The results are the same , in other words , Assign values to other elements first , b
Lowest priority , Sometimes you can't get value *.

<>4. Attention ：*b Cannot be assigned directly

There is one thing we need to pay attention to ,*b It cannot be assigned directly ：

The previously assigned object can only be the sequence type of list or tuple , and *b It does not belong to this type when it exists alone .

How can we correct it at this time ? It's simple , Just turn the front into a tuple or list ：

Even with a comma ,*b Can become tuples .

<>5. case

Then we will also take a case of unpacking the extended sequence , Let's use the list mentioned above [1,2,3,4,5] Successive reduction 1 Select an element and output the list , This time we use the sequence unpacking method ：
l = [1,2,3,4,5] while l: a,*l = l print(l)
Operation results ：
[2, 3, 4, 5] [3, 4, 5] [4, 5]  []
So we also talked about the content of the previous high-level assignment statement for Example of circular output of two-dimensional list , that for Can I also use a belt * To assign a value to a parameter ? The answer is OK ：

<> four , Multi-objective Assignment

Form of Multi-objective Assignment ：
a = b = c = 10

This too Python A point of elegance , In the underlying logic, the right side of the equal sign is assigned to the left first , Sequential assignment , Shared is 1 First address , in other words 10 Assign to c,c The value of becomes 10,c Reassign to b,b Reassign to a, Their addresses are the same ：

<> five , Enhanced assignment

Enhanced assignment operation += , We also talked about the foundation ：
+= -= *= /= //= %= **=
Their role is mainly ：
1. High execution efficiency 2. For system optimization 3. Reduce the amount of code
I won't give an example here , We often use this , It's also very simple , Just know that they have this effect .

Technology
Daily Recommendation