Python full stack development: Python ternary expressions, recursion, anonymous functions

Source: Internet
Author: User

Three-dimensional expression

Basic Syntax format

if else is false when the result  

Compares a large value in two numbers and returns

 #一般函数的写法
defmax2 (x, y):ifX >y:returnxElse: returnYRES=MAX2 (10,11)Print(RES)#结果x=12y=11#------------------------# ternary expressions are applied only to:# 1. The condition is established returns a value# 2. Condition not established returns a value res=x if x > y else y
Print (res)#升级版后的函数defmax2 (x, y):returnXifX > YElseyPrint(Max2 (10,11))
Recursive one, what is recursion

Recursive functions: Recursive invocation of functions, that is, in the process of function calls, the function itself is called directly or indirectly

# Call directly def foo ():     Print ('fromfoo')    foo () foo ()
# Indirect invocation def Bar ():     Print (' frombar')    foo ()def  foo ():    Print ('fromfoo')    bar () foo ()
Second, recursion is divided into two stages, recursion and backtracking

1. Recursion: recursive function Layer-deep process is the process of recursion

2. Backtracking: The process by which a recursive function returns results after satisfying the end condition.

Summarize:

1. Recursion must have a definite end condition

2. The size of the problem should be reduced every time the next recursion is entered

3. No tail-recursive optimizations in Python

anonymous functions

What is anonymous function keyword lambda

anonymity means no name . def func (x,y,z=1):    return x+y+z anonymous lambda# has the same scope as a function , but anonymity means that the reference count is 0 and is used once to release, unless it has a name func=Lambda x,y,z=1:x+y+z func (All-in-one)# It makes no sense to have a name .

Summarize:

1. The purpose of anonymity is to have no name, it doesn't make sense to assign a name to an anonymous function.

2. The parameter rules and the scope relation of the anonymous function are the same as the famous function.

3. The function body of an anonymous function should normally be an expression that must have a return value

Python full stack development: Python ternary expressions, recursion, anonymous functions

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.