"One: The role of *,-1"
Give you two examples:
1 a,b,c,d= " spam " 2 print (a) 3 print (b) 4 print (c) 5 print (d) 6 a,*b= " Spam " 7 print (a) 8 print (b)
The above example is the most simple example of the function of the * number, which differs from the pointer in the C language. Line Six, a assigns the s,b to Pam.
1 seq='computer'2 *a,b=seq3print(a) 4print(b)5 c,d=seq[:-1],seq[-1]6Print (a)7print(b)
The above is an example of a 1 effect that you can experience
Note: If there is more than one name with an asterisk, or if the value is less than the name with an asterisk, and if the name with an asterisk is not written to a list by itself, an error will be raised
"Two: Sequence assignment"
1 a=12 b=23 a,b=b,a4print(a)5Print (b)
Here, the original group on the right side automatically remembers the value of the previous variable
"Three: For Loop"
1 l=[1,2,3,4]2 m=[1,2,3,4]3 while L:4 Front,l=l[0],l[1:]5 print(front,l)6 7 while M:8 front,*m=M9 print( FRONT,M)
As can be seen from the example, the result of the next two pieces of code is the same, this is the role of *
1 for inch [(1,2,3,4), (5,6,7,8)]: 2 A,b,c=all[0],all[1:3],all[3]3 print(a,b,c)
"Four: Append"
1 a=b=[]2 b.append (3)print(A, b)45 a=[]6 b=[]7 b.append (8)print(a , b)
The first piece of code, because A and B refer to the same object, by B in the original value up, and we will see the same effect through a. The second piece of code has a different effect. Only append has this function.
"Five: Strong assignment Statement"
A strong assignment statement is similar to x+=x,y+=y in C, but it is important to note that there are no operators like X + + in Python
1 s='spam'2 s+='spam'3Print (s)
When a strong assignment statement is used for a string, the enhanced form is changed to perform the merge operation
Advantages of a strong assignment statement:
1, the left only need to calculate once, in X+=x, X can be complex object expression. In the enhanced form, you only need to calculate once. However, in full form x=x+y, X appears two times and must be performed two times. Therefore, enhanced assignment statements are typically performed faster.
2. Python automatically calls a faster method when we use an enhanced assignment statement
1 a=[1,2,3]2 b=[1,2,3]3 a.extend ([4,5])4 b=b+[4,5 ]5print(a)6print(b)
In the above example, the result of the next two pieces of code is the same, but the. Extend method is a bit faster. And then we'll look at the following example
1 a=[1,2,3]2 a+=[4,5]3print(a)
This example calls for a faster. Extend method, although it is not visible on the surface, but inside Python is called a more efficient method.
Attention:
1 l=[1,2]2 m=L3 l=l+[3,4]4print(m,l) 56 l=[1,2]7 m=L8 l+=[3,4]9 Print(m,l)
The differences of the above two examples let's experience
+ = can make changes to the list
1 m=l=[1,2]2 l.append (3)3print(m,l)4 5 m=l=[1,2]6 l=l+[3]7print(m,l)
Feel the effect of append again.
Getting Started with Python 2 (assigned value)