The fourth week of the Python course highlights records

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.