Some Understanding of return and print in python, pythonreturnprint
Preface
I recently started to learn python. I only had some c basics and I forgot about it. Now I want to learn and share my ideas ~ I read the return usage while reading the book, but it is mixed with print later. I always feel that the return value of the function can be directly displayed, but this is not the case. Next, let's take a look at the understanding of return and print in python. Let's take a look at the detailed in
1. Anonymous functionsAnonymous functions: (1). No Name (2). function comes with return# Common Functions def func (x,y,z=1): return x+y+zprint(func)# Lambda anonymous function f=lambda x,y,z=1:x+y+zprint(f ()# F=lambda x,y,z=1:x+y+z in the keyword lambda represents an anonymous function,# Colon: The previous x, Y, z indicates that they are parameters of this function.# An anonymous function does not n
(m_name) print ( m) print (M.path) sys=__import__ (' sys ') print (SYS)() round ()#!/usr/binl/env Python#encoding:utf-8#author:yangleiprint (Round (3.565,2)) print (round (3.555,2))Second, anonymous function1. DefinitionAnonymous functions:(1) No name.(2) The function body comes with return.Application Scenarios for anonymous functions:Apply to a one-time scenario for temporary use.1. Role#!/usr/binl/env python#encoding:utf-8#author:yangleidef func (x,y,z=1): return X+y+
#定义一个函数与变量的定义非常相似, for a well-known function, you must access it through the variable namedef func (x, y, z = 1):return x + y + Z#匿名函数: 1. No name, 2. Function body comes with return#匿名函数的应用场景, Temporary useLambda x, z = 1:x + y + ZPrint (lambda x, z = 1:x + y + z)f = lambda x, y, z = 1:x + y + ZPrint (f)F (a)#内置函数#1. ABS ()Print (All ([up to, ' a ', None])) #表示可迭代对象取出的每一个值的布尔值都为真Print (All ([])) #可迭代对象为空的列
, y, z:x + y + zprint(f (2, 3, 4))>>>9Lambda expressions are often used in conjunction with the map () and filter () functions, and we'll learn about them next.Map (): Map in sequenceThe map function applies the passed-in function to each element in a sequence object, and returns a list containing the result of the function call that was used.L=list (Map ((Lambda x:x+2), [1,2,3,4])print(l)>>>[3, 4, 5, 6]L=list (Map (pow,[1,2,3],[5,6,7))print(l)>>>[1,
anonymous function lambdaLambda Argument1,argument2,... argumentn:expression using arguments1, Lambda is an expression, not a statement.Because of this, lambda can appear where the DEF is not allowed in the Python syntax---for example, in a list constant or in the parameters of a function call.2. The body of a lambda is a single expression, not a block of code.Lambda is designed for writing simple functions, and DEF is used to handle larger tasks.For example:Lambda notation:F=lambda X,y,z:x+y+
Example of a simple adorner:def servlet(func): print("into servlet")#1 print(servlet)#2 def foo(): print("into foo")#7 print(func)#8,真正的bar函数 func()#9 print("out foo")#13 print(foo)#3 print("out servlet")#4 return foo@servletdef bar(): print("in old bar")/#0 print(bar)#11 print("out old bar")#12print(bar)#5,已经被装饰器装饰了bar()#6Execution sequence as above, execution result is as followsinto servletExamples of variable-parameter adorners:def desc(
value is always None.the difference between the eval () function and the EXEC () function: the eval () function evaluates only the value of a single expression, and the Exec () function can run code snippets dynamically. The eval () function can have a return value, and the EXEC () function return value is always none. X= 10deffunc (): Y= 20a=exec("X+y") Print("A:", a) b=exec("X+y",{"x": 1,"y": 2}) Print("B:", b) C=exec("X+y",{"x": 1,"y": 2},{"y": 3,"Z": 4}) Print("C:", c) d=exec("prin
needs to be specified. It must be a string or a code object. If object is a string, the string is first parsed into a set of Python statements and then executed (unless a syntax error occurs). If object is a code object, it is simply executed.
Globals: An optional parameter that represents the global namespace (which holds the global variable) and, if provided, a Dictionary object.
Locals: An optional parameter that represents the current local namespace (which holds local variables) an
).1.1 By Location: 20,18 is the argument, x, y is the formal parameter, and the corresponding relationship is 20,y or 18.Copy Code def func (x, y ): if x>y: return True Else:return Falsea=func (20,18)print(a)1.2. By keyword: When the argument is passed to the parameter, the 18 is passed to the Xdef func (x, y):Passfunc (y=20,x=18) 1.3 Mixing: The keyword parameter is always behind the position parameter.def func (a,b,x,y):PassFunc (15,25,y=20,x=18)1.4 Ternary algorithm: if X>y z
Import Random # Number Import stringRandom decimalsPrint (Random.random ())0.8681861054821751Random Print in 1-5 rangePrint (Random.randint (1,5))1and Randint difference in 1-4 range random printPrint (Random.randrange (1,5))3Randomly take 5 numbers out of this range and print them in list mode.Print (Random.sample (5))[+, 94, +] Print (Random.sample ('abcdef', 5)) ['b' D ' ' F ' ' e ' ' C ']Print 0-9 numbersPrint (string.digits) 0123456789Print A-ZPrint
use cases:#Multi-parameter casePrint("Multi-parameter case") Multi=Lambdax,y,z:x+y+ZPrint(multi ())#working with non-anonymous functionsPrint("working with non-anonymous functions")defNamedfunc (n):return Lambdax:n+xPrint(Namedfunc (2))#will print out function, equivalent to lambda x:2+xPrint(Namedfunc (2) (3))#will print out 5F= Namedfunc (2)Print(f (3))#equivalent to Namedfunc (2) (3)The results are as follows:Some small suggestions for lambda expr
'
--- 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 'y> Z'Else print 'z> y'
-- CaseUse 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 breakDeclare @ x int @ Y int @ C intSelect
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-- ProgrammaticSQLDeclare -- 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 -- pr
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.