The basic operation of an assignment statement has already been mentioned in the previous chapters, so let's say assignment to advanced applications
The first section is about sequence assignment.
Above is a simple sequence decomposition assignment statement, note: In the third row c,d=a,b is actually composed of a tuple, the right value is assigned to the left variable
The above-mentioned tuple usage techniques are often used
Moreover, the above technique is not only suitable for the entire tuple, but also suitable for partial assignment
Advanced Sequence Assignment Statement pattern
We can use = to mix the sides together, but the number of elements on the right must be the same as the left.
>>> a,b,c,d= ' ABCD ' >>> a,b,c,d (' A ', ' B ', ' C ', ' d ') >>> a,b,c= ' ABCD ' Traceback (most recent call Last): File ' <pyshell#20> ', line 1, in <module>
Sequence expansion pack
In the above we see that the left and right sides of the number must be the same, but there are other ways to break this
We can use * as a wildcard to represent the remaining data items
The above a is just the first item, the rest is assigned to B
Similarly:
This sequence extension package is valid for all sequence types
Note that there is a boundary condition where Python automatically assigns an empty value to an item that is out of bounds
It is also important to note that the * symbol can only have one, and only one variable is not available *
>>> a,*b,*c,d,*e= ' ABCD ' Syntaxerror:two starred expressions in assignment>>> a,b,c,d,e= ' ABCD ' Traceback (most recent): File "<pyshell#46>", line 1, in <module>
If a variable needs to be added *, then the variable must be followed by a comma, followed by an empty variable
Right here, thank you.
------------------------------------------------------------------
Click to jump 0 basic python-Catalogue
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
0 advanced application of python-10.1 sequence assignment in basic science