python--function 20, ternary expression, list derivation, generator expression

Source: Internet
Author: User

1, three-dimensional expression

#What is ternary expression ternary: three elementsx=1y=2ifX>y:Print(x)Else :    Print(y)#becomes ternary expression:Hhh=xifX>yElseyPrint(HHH)#Simple and clear————————————————————————————————————defmax2 (x, y):ifX>y:returnxElse :        returnyPrint(Max2 (1,5))#becomes ternary expression:defmax2 (x, y):returnXifX>yElseyPrint(Max2 (1,5))#Can see the ternary expression can be output with a line of the result is very simple!!!! 

2. List Deduction formula

l=[1,25,35,60,85,96]l_new=[]l_new2=[] forIinchL:ifI >50: L_new.append (i)Else: L_new2.append (i)Print(l_new)Print(L_NEW2)#become list-derivedl=[1,25,35,60,85,96]x=[i forIinchLifI < 50]y=[i forIinchLifI < 50]Print(y)Print(x)

3. Generator expression

# change the list's [] to () is the generator expression  for  in rangeprint(x.__next__())print(x.__next__  ())print(next (x))# you can see x.__next__ () =next (x)

These three expressions can be seen as the function of making the code more neat and simple and convenient

python--function 20, ternary expression, list derivation, generator expression

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.