Working with generators of Python__python

Source: Internet
Author: User
Tags generator readline
#enumerate #seasons =[' Spring ', ' Summer ', ' Autumn ', ' Winter '] seasons= (' Spring ', ' Summer ', ' Autumn ', ' Winter ') # env tuple  is works, but after enumerate it would be List print seasons Print List (enumerate (seasons)) #[(0, ' Spring '), (1, ' Summer '), (2, ' Autumn '), (3, ' Winter ')] Print List (enumerate (seasons,start=1)) #[(1, ' Spring '), (2, ' Summer '), (3, ' Autumn '), (4, ' "  Winter ')] # Eval Function why???? What can I do and fun x =1 print eval (' x==1 ') # True Print eval (' x + 1 ') print x +1 #iter functions with open (' Myfil E.txt ') as Fh:for line in ITER (Fh.readline, ""): # Fh.readline () not works here Print line fh2 = open ("Myfil  E.txt ") # for personal I like this one, it very mor clear for line in FH2: # It's works print line #Nested Sequences plain =[' spring ', ' Autumn ', ' Summer ', ' Winter '] print plain[1][1] # u sea = [(' Alxs ', ' home ']] Print sea print Sea[0][0] #alxs print sea[0][0][0] # got a #generator functions def main (): print ' This is a smaple gEnerator function ' For I in Range ': Print (i) # not got print Inclusive_range (0,25,1) #<gener Ator object Inclusive_range at 0x00000000024d0510> to I in Inclusive_range (0,25,1): # This function has no connect Ion with function range print i print '---------------------------------------' #o =inclusive_ranges () # Typeerror:requires at least 1 param #one =inclusive_ranges (6) #two =inclusive_ranges (1,4) #three =inclusive_r Anges (2,7,2) four =inclusive_ranges (1,4,6,8) #TypeError: expected at most 3 param received 4 to I in four:p ri
        NT I def inclusive_range (start,stop,step): i = start while I <=stop:yield i #print i+100 I+=step #using gnerators with classes class Inclusive_ranges:def __init__ (Self,*args): Numargs =len (AR GS) If Numargs < 1:raise typeerror (' requires at least 1 param ') elif Numargs = = 1:self.
           Stop=args[0] Self.start = 0 self.step =1 elif numargs ==2:self.step = 1 Self.start = args [0] Self.stop =args[1] # (self.start,self.stop) =args elif Numargs ==3: (self.s
    Tart,self.stop,self.step) =args else:raise typeerror (' expected at most 3 param received {} '. Format (Numargs)) 


def __iter__ (self): I =self.start while I <= self.stop:yield i i = self.step Main ()

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.