Python: Functions

Source: Internet
Author: User

Functions are well-organized, reusable pieces of code that are used to implement a single, or associated function.

Define and Invoke
def Add (x, y): ...      Print ('x=', x) #Python3必须加 "     ()" ... Print ('y='     , y) ... return x+y ... >>> Addx= 1y= 23
Parameter collection

#当参数不确定时, use * to define a function and return a tuple

 def  func (x,*arg):  print   (x) result  =x  print   for  i in   Arg:result  +=i  return   result  print  (func ( 1,2,3,4,5,6,7,8,9)) 
# returns the result 1    # corresponding to print (x)(2, 3, 4, 5, 6, 7, 8, 9)        # corresponding to print (ARG)    # Final Print (func (1,2,3,4,5,6,7,8,9)) Results                   

#当参数为赋值语句时, use * * to define the function and return a dictionary

def F (* *Karg): ...      Print (Karg) ... >>> f (a=1,b=2,c=3)        # When the argument is a non-assignment statement, the error {'C' a ' ' b ': 2}
* and * * Define function Synthesis Example
>>>defFoo (x,y,z,*arg,**karg): ...Print(x) ...Print(y) ...Print(z) ...Print(ARG) ...Print(Karg) ...>>> Foo ('Taylor', 1989,'Adele') Taylor1989Adele () {}>>> Foo (1,2,3,4,5)123(4, 5){}>>> Foo (1,2,3,4,5,name="Taylor")123(4, 5){'name':'Taylor'}
Summary of several definition methods

1. def a (P1,P2,P3) #参数的位置很重要
2. Def a (P1=V1,P2=V2) #定义的时候直接赋值
3. Def a (*arg) #适用不确定个数
4. Def a (**arg) #必须接收arg =val form

Several function lambda
>>> num=[1,2,3,4,5]>>> lam=Lambda x:x+1    # function directly after using the variable, After the variable is a colon-isolated expression, the result of the expression is the return value of the function >>> n=[] for in num: ...     N.append (Lam (i)) ... >>> n[2, 3, 4, 5, 6]
Map

Map (FUNC,SEQ), which executes the Func function on each element of an iterative object

>>> num=[1,2,3,4,5]>>> list (map (Lambda x:x+1,num))    #  Python3 need to convert map to list format, otherwise error, because direct use of map returned is iterators[2, 3, 4, 5, 6]
>>> l1=[1,2,3,4,5]>>> l2=[9,8,7,6,5]>>> list (map (lambda x, y: x+y,l1,l2)) [10, 10, 10, 10, 10]
Reduce

Reduce (FUNC,SEQ), Func must be a two-element operation function, first of all, the 1th, 2 data in the collection operation, the resulting result and the third data with the Func () function, and finally get a result. Horizontal operation.

 from import reduce   #Python3 cannot directly use the Reduce function, first refer to >>> reduce (lambda x,y:x +y,[1,2,3,4,5])     #"Sideways" calculated in turn 15
Filter

Filter (func, iterable), performs a func on the element in iterable and returns the element that satisfies func to the new list

>>> l=[1,2,3,4,5]>>> list (filter (lambda x:x>3,l))        #  Same reduce function [4, 5]

  Equivalent statements

 for inch if x>3]        # ternary operator     [4, 5]#ifelse  Z If X is true, execute a=y if X is false, perform a=z

Python: Functions

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.