1、 Exchange
a=1
b=2
temp=a
a=b
b=temp
Equivalent to
a,b=b,a
In the previous sentence, the right side of the equal sign uses encapsulation, while the left side uses deconstruction
2、 Deconstruction
1. Solve the elements of linear deconstruction and assign them to other variables in order;
2. The number of variables accepted on the left should be consistent with the number of elements solved on the right.
give an example:
lst=[3,5]
first,second=lst
print(first,second)
a,*b=range(10)
#表示a=0,b=剩下的列表
a,*b,c=range(10)
#表示a=0,c=9,b=剩下的列表
a,[b,c],d=[1,[2,3],4]
Use * variable name to receive, but cannot be used alone;
It is collected by * variable names to form a list.
Discard variable
This is a convention, an unwritten provision, not a standard;
If you don't care about a variable, you can define the variable name as _;
_ It is a legal identifier, and can also be used as a valid variable. However, if it is defined as underlined, it is hoped that it will not be used unless you clearly know that the data needs to be used.
give an example:
h,*_,t=range(10)
#
"_" generally represents the variable that the user does not care about, realizing the purpose of discarding the variable
h,t