Python Learning Notes (16) Looping objects

Source: Internet
Author: User
Tags function definition

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Original chain: http://www.cnblogs.com/vamei/archive/2012/07/09/2582499.html

Note: There is a more awkward noun; table derivation, Generator Gen (), iterator (the job is not covered in a large space)

#16th Cycle Object forLineinchOpen'D:/pythonfiles/test.txt'):    Print(line)#output: print by row#1234#ABCD#EFG#The author means, should be the manual cycle, and then loop to the back, appear Stopinteration,python think the loop is done#for loop is to automatically call the next () method until Stopinteratrion appears#GeneratordefGen (): a= 100yielda A= A*8yieldayield1000 forIinchGen ():Print(i)#Output:# -# +#Gen () is like a function definition, and return is changed to yield.#can have multiple yield#Gen () pauses to run the generator after yield, returns the yield value, and when called again, resumes from the time of the pause to the next yield#the generator itself forms a circulator, with each loop using a yield return valuedefGen (): forIinchRange (4):        yieldI#Builder ExpressionG = (x forXinchRange (4))#G.next ()#Print Elements#Python2 can be implemented with. Next ()#Python3 Error, the attribute is wrong, because there is no such methodNext (G)#can be typed in sequence until the Stopinteration,loop is over.#Output" ">>> Next (g) 0>>> next (g) 1>>> next (g) 2>>> next (g) 3>>> next (g) Traceback (most recent): File "<pyshell#830>", line 1, in <module> next (G) stopiteration" "#Table DerivationL = []#L an empty list forXinchRange (10):#Loop to 9,range no upper limitL.append (x**2)#L increase x square at tailL#output: [0, 1, 4, 9, +,----L= [X**3 forXinchRange (10)]#output: [0, 1, 8, 216, 343, 729]#Job:XL = [1,3,5]yl= [9,12,13]l= [x**2 for(x, Y)inchZip (xl,yl)ifY > 10]#First ConversionXL = [1,3,5]yl= [9,12,13]l= [] for(x, Y)inchZip (xl,yl):ifY >10:        Print(x**2) L#Output 9#but not inside the list .XL= [1,3,5]yl= [9,12,13]l= [] for(x, Y)inchZip (xl,yl):ifY >10: L.append (x**2) L#output changed [9]Zip (xl,yl)#the output is (1, 9) (3, a) (5 )#the IF condition statement y>10 the square of the X#y is greater than 10, the second two, 3 squared, and 5 squared .#so the output should be [9,25]

Python Learning Notes (16) Looping objects

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.