1. iterators
names = iter (["Alex","Jack","rain" ]) # declares an iterator for the list names. __next__# Iteration
2. Generator, After using yield, the function becomes a generator, and the calling function returns a Iterable object
defCash_monkey (amount): while(amount>0): Amount-= 100Print("take the money.") yieldAMOUNTATM= Cash_monkey (500)Print("still left%s"%atm.__next__())#The __next__ () method is equivalent to executing a function once, and stopping at the yield statement returns yield equivalent to returnPrint("still left%s"%atm.__next__()) forIinchCash_monkey (500):#the __next__ () method is called automatically in the For loop Print(i)#using in read filesdefRead_file (Fpath): Block_size=1024with open (Fpath,'RB') as F: whileTrue:block=F.read (block_size)ifBLOCK:yieldBlockElse: returndefh ():Print('Wen Chuan') M=yield5#received the fighting! sent by send . Print(m) d=yield12Print('We are together!') C=h () m= c.__next__()#m Gets the parameter value of yield 5 5D = C.send ('fighting!')#d Gets the parameter value of yield 12Print('We'll never forget the date'M'.', d)
3. Two-dimensional array
" " [0,1,2,3][0,1,2,3][0,1,2,3][0,1,2,3]----------[0,0,0,0][1,1,1,1][2,2,2,2][3,3,3,3] " " for in a range (4)] for in range ( Len (data): for in range (4 )] print(a)
4. Regular expressions
Importrematch= Re.match (r'Dog','Dog Cat Dog')#Match startMatch.group ()#get the element on the matchMatch = Re.search ('Dog','Dog Cat Dog')#match any position, match only onceMatch.group () Match.start ( )#The start position of the element on the matchMatch.end ()#The end position of the element on the matchMatch = Re.findall (r'Dog','Dog Cat Dog')#all matching objects, get a listContactInfo ='Doe, john:555-1212'Match= Re.search (r'( \w+), (\w+): (\s+)', ContactInfo)#divided into 3 sub-stringsMatch.group (1)#DoeMatch.group (2)#JohnMatch.group (3)#555-1212Match.group (0)#match all Doe, john:555-1212
Grammar:
The fourth week of the Python course highlights records