Python learning Experience (iv) yield and ternary operations and lambda expressions

Source: Internet
Author: User
Tags generator xpath

1.yield meaning of production and generation, the function with yield in Python is called the Generator (Generator)

The larger the #生成10000个元素的List value of the ' for I  in range ', the larger the memory consumed in the run will be 10000 for J in Xrange (10000) #xrange返回的不是List, but a Iterable object Each iteration returns the next value, taking up a very small amount of memory space "Def Readfilebyyield (XPath): With    open (XPath, ' RB ') as Ty: For line in        Ty.readlines ():            yield Line #每执行一次yield, the statement is interrupted, and an iteration value is returned #for the loop automatically calls next () for line in Readfilebyyield (' D:\wangyi.py '):    print Line

2.Python ternary operation Syntax format: The result of true if the condition is determined if the conditions else is false  

1 if 10>9 else 0def threeelementexpression (A, a):                                 return A-B if a > B. Else ABS (A-a) print threeelementexpress Ion (1,5) print threeelementexpression (10,5)

The 3.LAMBDA expression, which functions as a function sketch, is equivalent to an anonymous function, whereas a function declared with DEF is named, such as:

Lambdatest = Lambda x:x**2for i in range (1,5):    print lambdatest (i) List_lambda = Range (1,10) #列表中的每个元素加1print map ( Lambda a:a+1, List_lambda)  #返回列表中的偶数print filter (lambda a:a% 2 = = 0, List_lambda)  

  

  

Python learning Experience (iv) yield and ternary operations and lambda expressions

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.