Python3 generator and python3 Generator

Source: Internet
Author: User

Python3 generator and python3 Generator

1 ''' 2 list generation type 3''' 4 5 # a1 = [x for x in range (10)] 6 # a2 = [x * 2 for x in range (10)] 7 # print (a1) 8 # print (a2) 9 #10 # def fun1 (x ): 11 # return x ** 3 12 # a3 = [fun1 (x) for x in range (10)] 13 # print (a3) 14 #15 # list generation type 16 # Value Method 17 # B = ['asd ', 8, 9, 10] 18 # w, x, y, z = B 19 # print (w, x, y, z) 20 # print (B) 21 #22 #23 # generator 24 # c = (x * 2 for x in range (10) # c is the generator object 25 # print (c) # <generator object <genexpr> at 0x00000286BEF21CA8> 26 # print (next (c) # equivalent to c. _ next _ (), medium price in Python2 to c. next () 27 # print (next (c) 28 # print (next (c) 29 # print (next (c )) #30 #31 # the generator is an iteratable object 32 # import time 33 # for I in c: # for a next () function 34 # print (I) 35 ## time. sleep (1) 36 37 ''' 38 generator two creation Methods 39 1. (x * 2 for x in range (10) 40 2. yield 41 ''' 42 43 # def fun2 (): 44 # print ('###') # This print is not shown 45 # yield 1 46 # print (fun2 ()) # fun2 () is the generator object 47 #48 # def fun3 (): 49 # print ('###') 50 # yield 1 51 #52 # print ('asd ') 53 # yield 2 54 #55 # return None 56 #57 # c1 = fun3 () 58 # next (c1) # next () is returned 1 59 # next (c1) # next () is returned 2 60 #61 # for I in fun3 (): 62 # print (I) # '###', 1, 'asd ', 2 63 # At this time, 1 and 2 are the above returned values 64 65 66 ''' 67 what is an Iterated object, the iteratable object has the iter method 68 ''' 69 70 # list1 = [, 3] 71 # list1. _ iter _ () 72 #73 # tup1 =, 3) 74 # tup1. _ iter _ () 75 #76 # disc1 = {'asd ': 123} 77 # disc1. _ iter __() 78 79 80 81 # Fibonacci sequence 82 #0 1 1 1 2 3 5 8 13 21 34 83 # def fun4 (max): 84 # q, before, after = 0, 0, 1 85 # while q <max: 86 # print (after) 87 # before, after = after, before + after 88 # q + = 1 89 # fun4 (10) 90 # print (fun4 (8) 91 92 # Use the generator to create the Fibonacci series 93 # def fun5 (max): 94 # q, before, after =, 1 95 # while q <max: 96 #97 # yield before 98 # before, after = after, before + after 99# q = q + 1100 # z = fun5 (8) 101 # print (z) 102 # print (next (z) 103 # print (next (z) 104 # print (next (z )) 105 # print (next (z) 106 # print (next (z) 107 108 109 ''' 110 send method 111 ''' 112 # def fun5 (): 113 # print ('a1') 114 # a = yield 1115 # print (a) 116 #117 # yield 2118 # x = fun5 () 119 # x1 = x. send (None) # equivalent to next (x) 120 # No next before the first send, only one send (None) 121 # h = x. send ('aaaaaaaaaaa') 122 # print (h) 123 124 ''' 125 send is more than next. You can input a parameter to the variable 126 '''

 

Related Article

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.