Learn python syntax from code six (continuous update)

Source: Internet
Author: User

#-*-coding:utf-8-*-classMyClass:"""A Simple Example Class"""I= 12345def __init__(Self, L, R): Self.data=[] SELF.L=L SELF.R=Rdeff (self):return 'Hello World'Print(MYCLASS.I)Print(MyClass.__doc__) x=myclass ()Print(X.data)Print(X.L)classBag:def __init__(self): Self.data= []    defAdd (self, x): Self.data.append (x)defAddtwice (self, x): Self.add (x) self.add (x) s='ABC'it=ITER (s)Print(Next (IT))#This form of access is clear, concise and convenient. #the use of iterators is common and uniform in Python. #in the background, the For statement calls ITER () in the container object. #This function returns an iterator object that defines the next () method .#, which accesses the elements in the container. #S=iter (BAG)classReverse:"""Iterator for looping over a sequence backwards."""    def __init__(self, data): Self.data=Data Self.index=len (data)def __iter__(self):return Selfdef __next__(self):ifSelf.index = =0:Raisestopiteration Self.index= Self.index-1returnSelf.data[self.index]rev= Reverse ('spam') K=ITER (REV)Print(Next (k))Print(Next (k))#Print (next (k))#Print (next (k))#Print (next (k)) forCharinchRev:Print(char)#Generator is a simple and powerful tool for creating iterators. #They're written like normal functions,#Use the yield statement when you need to return data. #each time next () is called,#the generator replies to its detached position (it remembers where the statement was last executed and all the data values). #The following example shows that the generator can be easily created:defreverse2 (data): forIndexinchRange (len (data)-1,-1, 1):        yieldData[index] forCharinchReverse2 ('Golf'):    Print(char) Q= Reverse2 ("FF") P=ITER (q)Print( Next (P)) sum (i*i forIinchRange (10))#Sum of squaresXvec = [10, 20, 30]yvec= [7, 5, 3]sum (x*y forX, yinchZip (Xvec, Yvec))#dot Product

Learn python syntax from code six (continuous update)

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.