this place. Because DEF is a statement, not an expression that cannot be nested inside, a lambda expression can have only one expression after ":". That is, in Def, return can also be placed behind a lambda, and cannot be returned with return can not be defined behind a Python lambda. Therefore, a statement such as if or for or print cannot be used in a lambda, and lambda is generally used only to define simple functions.
Here are some examples of Python lambda:1. The case of a single parameter
ControlGrant -- Grant the user access permissionDeny-Deny User AccessRevoke -- revoke User Access Permissions-- Transaction controlCommit -- end the current transactionRollback -- abort the current transactionSET transaction -- define the data access features of the current transaction-- Programmatic SQLDeclare -- set the cursor for the queryExplain -- describe the data access plan for queryOpen -- open a cursor for retrieving query resultsFetch -- retrieve a row of query resultsClose -- close
the database
-- Data ControlGrant -- Grant the user access permissionDeny-Deny User AccessRevoke -- revoke User Access Permissions
-- Transaction controlCommit -- end the current transactionRollback -- abort the current transactionSET transaction -- define the data access features of the current transaction
-- Programmatic SQLDeclare -- set the cursor for the queryExplain -- describe the data access plan for queryOpen -- open a cursor for retrieving query resultsFetch -- retrieve a row of query
the user access permissionDeny-Deny User AccessRevoke -- revoke User Access Permissions-- Transaction controlCommit -- end the current transactionRollback -- abort the current transactionSet transaction -- define the data access features of the current TRANSACTION-- Programmatic SQLDECLARE -- set the cursor for the queryExplain -- describe the data access plan for queryOPEN -- OPEN a cursor for retrieving query resultsFETCH -- retrieve a row of query resultsCLOSE -- CLOSE the cursorPREPARE -- p
the user access permissionDeny-Deny User AccessRevoke -- revoke User Access Permissions-- Transaction controlCommit -- end the current transactionRollback -- abort the current transactionSET transaction -- define the data access features of the current transaction-- Program SQLDeclare -- set the cursor for the queryExplain -- describe the data access plan for queryOpen -- open a cursor for retrieving query resultsFetch -- retrieve a row of query resultsClose -- close the cursorPrepare -- prepar
-- Data Control
Grant -- Grant the user access permission
Deny-Deny User Access
Revoke -- revoke User Access Permissions
-- Transaction control
Commit -- end the current transaction
Rollback -- abort the current transaction
SET transaction -- define the data access features of the current transaction
-- Programmatic SQL
Declare -- set the cursor for the query
Explain -- describe the data access plan for query
Open -- open a cursor for retrieving query results
Fetch -- retrieve a row of query re
ControlGrant -- Grant the user access permissionDeny-Deny User AccessRevoke -- revoke User Access Permissions-- Transaction controlCommit -- end the current transactionRollback -- abort the current transactionSET transaction -- define the data access features of the current transaction-- Programmatic SQLDeclare -- set the cursor for the queryExplain -- describe the data access plan for queryOpen -- open a cursor for retrieving query resultsFetch -- retrieve a row of query resultsClose -- close
-- Data Control
GRANT -- GRANT the user access permission
DENY-DENY User Access
REVOKE -- REVOKE User Access Permissions
-- Transaction control
COMMIT -- end the current transaction
ROLLBACK -- abort the current transaction
Set transaction -- define the data access features of the current TRANSACTION
-- Programmatic SQL
DECLARE -- set the cursor for the query
Explain -- describe the data access plan for query
OPEN -- OPEN a cursor for retrieving query results
FETCH -- retrieve a row of query re
row of query resultsCLOSE -- CLOSE the cursorPREPARE -- prepare SQL statements for dynamic executionEXECUTE -- dynamically execute SQL statementsDESCRIBE -- DESCRIBE the prepared Query--- Local variablesDeclare @ id char (10)-- Set @ id = '20140901'Select @ id = '000000'--- Global variables--- It must start @
-- IF ELSEDeclare @ x int @ y int @ z intSelect @ x = 1 @ y = 2 @ z = 3If @ x> @ yPrint 'x> y' -- print the string 'x> y'Else if @ y> @ zPrint
function object, but it is discarded immediately, because you didn't use its return value, that is, the function object. Lambda is just an expression that can be directly used as a member of the python list or python dictionary, for example:
info = [lamba a: a**3, lambda b: b**3]
There is no way to directly replace it with the def statement. Because def is a statement, not an expression that cannot be nested in it, lambda expressions can only have one expression after. That is to say, in def,
! '
2.3 collect parametersIf the function can store multiple names, you can provide the function with any number of parameters. we need to do this: provide a parameter when defining the function and add an asterisk before it.
The code is as follows:
>>> Def print_para (* paras ):Print paras>>> Print_para ('SS ')('SS ',)>>> Print_para (1, 2, 3)(1, 2, 3)
The asterisks before the parameter place all values in the same tuples. it can be said that these "parameters in other locations" are collect
; @ YPrint ''x> y'' -- print the string ''x> y''Else if @ Y> @ ZPrint ''y> z''Else print ''z> y''
-- Case
Use panguUpdate employeeSet e_wage =CaseWhen job_level = '1' then e_wage * 1.08When job_level = '2' then e_wage * 1.07When job_level = '3' then e_wage * 1.06Else e_wage * 1.05End
-- While continue break
Declare @ x int @ Y int @ C intSelect @ x = 1 @ Y = 1While @ x BeginPrint @ X -- print the value of variable XWhile @ Y BeginSelect @ C =
sketch. Allows the definition of a function to be embedded within the code.--------------------------------------------------------------Lambda syntax: Lambda [arg1[,arg2,arg3....argn]]:expression1. For a single parameter:g = Lambda x:x*2Print g (3)The result is 6.2. Multiple parameters:m = lambda x, y, Z: (x-y) *zPrint m (3,1,2)The result is 4.3. No parametersf = lambda:'beginman' F () #beginman4. Lambda used in filter, map, reduce function.Print R
):returnName[0].upper+name[1:]defdaxie2 (name):return '%s%s'% (Name[0].upper (), name[1:]) A=['Linghuchong','Dongfangbubai']r=map (daxie2,a) forIinchr:Print(i)--reduceReduce, like the map function, accepts two parameters, but the reduce function calculates the result of the current value and the result of the next value in a cumulative way.i.e.: Reduce (f,[1,2,3,4]) =f (f (3), 4)Take a look at a simple example of the reduce function# fromFunctoolsImportReducedefLeijia (x, y):#Note that the funct
hello_1 (greeting = " hello , name = " world ): print %s,%s ' %def ' World ' ): print'%s,%s'%(greeting, name)>>> Hello_2 ('hi') Hi, World3) variable number of parametersdef print_params (*params): print params>>>print_ params (' testing') ('testing',)>>> Print_params (1, 2, 3) (1, 2, 3)As can be seen from the above example, a tuple is printed. If used in conjunction with common parametersdef Print_ params_2 (title, *params): print title Print params >>> print_para
-- delete a DOMAIN from the database-- Data ControlGRANT -- GRANT the user access permissionDENY-DENY User AccessREVOKE -- REVOKE User Access Permissions-- Transaction controlCOMMIT -- end the current transactionROLLBACK -- abort the current transactionSet transaction -- define the data access features of the current TRANSACTION-- Programmatic SQLDECLARE -- set the cursor for the queryExplain -- describe the data access plan for queryOPEN -- OPEN a cursor for retrieving query resultsFETCH -- re
variables print ( ========= Gorgeous split line ========= ) x = 2 # print (ID (x)) # print (x+5) # The name is the same, but the new variable x is usedContinue to assign value' Hello python ' Print (ID (x)) Print (x)At this point, X becomes a new variable, and the variable type is changed by the data type of the assigned value.Here, the ID () is a python built-in function. See also: Https://docs.python.org/3/library/functions.html#idIf the variable is not assigned a value, Python
to GLOBALDEFNBSP;FUN8 ():x =10deffun8_1 (): nonlocal x #3.0 later in this statement, the system knows that x is an external variable of fun8_1 x*= x #x试图被修改, because x belongs to an external variable of the Fun8_1 function, and the x system in Fun8_1 is considered an internal variable of fun8_1, it will not be found, resulting in an error returnxreturnfun8_1 () "Deffun8" (): x=[10] #列表在下方使用的时候不会被屏蔽 deffun8_1 (): x[0]*=x[0] #x试图被修改, since x belongs to the external variable of the Fun8_1 functio
function and add an asterisk before it.Copy codeThe Code is as follows:>>> Def print_para (* paras ):Print paras>>> Print_para ('ss ')('Ss ',)>>> Print_para (1, 2, 3)(1, 2, 3)The asterisks before the parameter place all values in the same tuples. It can be said that these "parameters in other locations" are collected before use. If no collection element is provided, the parameter returns an empty tuples ().However, to process the "Collection" Operation of keyword parameters, we need two asteris
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.