I. Assignment
#Sequence Unpacking>>> x, y, z = 1,2,3>>>Print(x, Y, z)1 2 3>>> dic = {'Jameson':'1001','Jack':'1002','Abel':'1003'}>>> Key,val =Dic.popitem ()>>>Print(key,val) Jack1002#Chained Assignment>>> A = b = c = 3>>>Print(a,b,c)3 3 3#Increment Assignment>>> str2 ="ABC">>> str2 + ="def">>>str2'abcdef'>>> str2 *= 3>>>str2'Abcdefabcdefabcdef'
Two. Conditional statements and other
#If,elif,elseifCondition_1:statement_block_1elifcondition_2:statement_block_2Else: Statement_block_3#comparison Operators"""x = = y x equals Yx < Y x is less than yx > y x is greater than Yx >= y x is greater than equals Yx <= y x is less than equals yx! = y x is not equal to Yx is y X and y is the same pair The members of the Y container (for example, the sequence) x not in Y X are not members of the Y container (for example, sequence), like x is not y x and Y are different objects x in Y x"""#Boolean operator (seemingly not in Python | | &&!) ) and or not#assertion (not explained)assert
Three. Loops and other
# whilex= 1 whileX < 4: Print(x) x+ = 1Output:123Count=0 whileCount < 5: Print(Count,"less than 5") Count= Count + 1Else: Print(Count,"greater than or equal to 5") output: 0 less than51 less than 52 less than 53 less than 54 less than 55 greater than or equal to 5# for for<variable>inch<sequence>: <statements>Else: <statements>##例子: forNumberinchRange (1,101):#Print 1~100 Print(number) dic= {'x':'1','y':'2','Z':'3'} forKeyinchDIC:Print(Key +":", Dic.get (key)) output: x:1Z:3y:2DiC= {'x':'1','y':'2','Z':'3'} forKey,valinchDic.items ():Print(Key +":", val) output: Z:3y:2x:1#break,continue slightly#The pass pass is an empty statement in order to maintain the integrity of the program structure. #Iterative Tools##zip (Zip can handle unequal sequences, the shortest sequence "runs out" when it stops)>>> a = ["x","y","Z"]>>> B = ["1","2","3"]>>>list (Zip (A, b)) [('x','1'), ('y','2'), ('Z','3')]#Join the LoopA = ["x","y","Z"]b= ["1","2","3","4"] forLetter,numinchZip (A, b):Print(letter,num) output: x1y2Z3##enumerate to a data object that can be traversed (such as a list, tuple, or string), enumerate combines the data object into an index sequence, listing both the data and the data subscript (okay, OK)A = ["x","y","Z"] forI,letterinchEnumerate (a):Print(I,a[i]) output: 0 x1y2Z#reversed and sorted flip and sort iterations>>> Sorted ("hello,woeld!")['!',',','H','D','e','e','L','L','L','o','o','W']>>> List (Reversed ("hello,woeld!"))['!','D','L','e','o','W',',','o','L','L','e','H']##while True slightly
Four. List derivation and pass, Del, exec
#list derivation is a way to create a new list with other lists>>> [x*x forXinchRange (10)][0,1, 4, 9, 16, 25, 36, 49, 64, 81]>>> [x*x forXinchRange (10)ifX% 3 = =0] [0,9, 36, 81]>>> [x+1 forXinchRange (10)][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> [(x, Y) forXinchRange (3) forYinchRange (3) [(0, 0), (0,1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2))] You can also>>> girls = ['Alice','Bernice','Clarice']>>> Boys = ['Charis','Arnold','Bob']>>> Lettergirls = {}>>> forGirlinchGirls:letterGirls.setdefault (girl[0],[]). Append (Girl)#you can still use it, I take it, check the next SetDefault (key[, default]) if key is in the dictionary, it returns its value. If not, insert a key with the default value and return to default. Originally returned to the list, no wonder can append! >>>lettergirls{'C': ['Clarice'],'b': ['Bernice'],'a': ['Alice']}>>>Print([B +"+"+g forBinchBoys forGinchLettergirls[b[0]]) ['Charis+clarice','Arnold+alice','Bob+bernice']
# The pass pass is an empty statement in order to maintain the integrity of the program structure. #del delete variable #exec and eval go back to the research.
Refer to "Basic Python Tutorial" (2nd edition) • Revision
Python basic syntax