Python Learning Path The next day-function

Source: Internet
Author: User

One, python2.x built-in function table:

Note: The above is pyton2. X built-in function, official website: https://docs.python.org/2/library/functions.html

Second, python3.x built-in function table:

Note: The above is Pyton3. X built-in function, official website:https://docs.python.org/3.5/library/functions.html

Three, custom functions:

def函数名(参数):

    ...

   函数体      ...
    • def: is a function keyword that tells the Python interpreter that this is a function.
    • Function Name: The name of this functions, and the identifier of this function is called in the code.
    • function Body: Some logical operations are performed in the function, for example: if judgment, for loop, function, etc.
    • Parameters: Providing data for this function body
    • Return value: Once the function has finished executing, it can return data to the caller. Example: Return.

1. function return value:

defx ():if1 <0:returnTrueElse:        returnFalseifx ():Print('1 less than 0')Else:    Print('1 greater than 0'The result of the previous instance running back is: 1 is greater than 0
function Inverse return value (return)

2. Pass parameters to the function (pass parameter):

The parameters of the function are divided into:

    • General parameters
    • Default parameters
    • Dynamic parameters

(1), General parameters:

def x (name):     Print (name) x: is the function name name: x is the formal parameter (also called the formal argument) of the functions x ('Earl') Earl is the actual parameter of the X function (also called an argument) after the instance is run, the result of the inverse return is: Earl
General Parameters

(2), default parameters:

def x (name, age=20):    print('name:%s,age:%s' %(name, Age) x: function name. Name: The form parameter for the X function. Age: The default parameter for the X function. X ('Earl') above call function to use default parameters, run back result result is: name:earl,age:x ( ' Earl ',more than the call function is not using the default parameters, after the run back result results are: Name:earl,age:26
Default Parameters

(3) Dynamic parameters:

defX (*args):Print(args) The first method of invocation is: X (1, 2, 3,'name'after the instance runs, the result of the reverse return is: (1, 2, 3,'name'The following is the second method of invocation: Lis= [1, 2, 3,'name',' Age',]x (*lis) After the instance is run, the result of the reverse return is: (1, 2, 3,'name',' Age')
Dynamic Parameter One
defX (* *Kwargs):Print(Kwargs) The following is the first method of execution: X (name='Earl', age=25after running the above instance, the result of the reverse return is: {' Age': 25,'name':'Earl'The following is the second method of execution: DiC= {'name':'Earl',' Age': 25,'Address':'Beijing'}x (**DIC) After the instance is run, the result of the reverse return is: {'Address':'Beijing',' Age': 25,'name':'Earl'}
Dynamic Parameter Two
Print('after running the above instance, the result of the reverse return is:')defX (*args, * *Kwargs):Print(Args,kwargs) The first method of invocation: X ([, name='Earl', agg=24after running the above instance, the result of the reverse return is: ([33, 44, 55],) {'agg': 24,'name':'Earl'The following is the second method of invocation: Lis= [1, 2, 3,'name']dic= {'name':'Earl',' Age': 25,'Address':'Beijing'}x (*lis, * *dic) The result of the inverse of the above instance after running is: (1, 2, 3,'name') {'Address':'Beijing',' Age': 25,'name':'Earl'}
Dynamic miserable number threesending an email instance

Iv. lambda expression:

When learning conditional operations, for simple if else statements, you can use the ternary operation to indicate that:

Ternary operations:

#Common conditional Statementsif1 = = 1: Name='Earl'Else: Name='Tom'Print('General judgment:%s'%name)#Ternary OperationsName ='Earl' if1 = = 1Else 'Tom'Print('ternary operation:%s'%name) After the instance is run, the result of the return result is: General judgment: Earl Ternary operation: Earl
Ternary Operations

For simple functions, there is also an easy way to represent them:

Lambda expression:

# defining functions (normal way) def x (ARG):     return arg + 1#  Execute function re = x (+)print(re) after the instance is run, the result of the reverse return is:101 # Defining a function (lambda expression) Lambda Arg:arg + 1#  Execute function re = x (+)print(re) After running the above instance, the result of the reverse return is:101
lambda expression

Five, map function:

Iterates through the sequence, operates on each element of the sequence, and finally gets a new sequence.

Li = [one, one, one, one = map (lambda a:a +, li)print(list (new_list)) and the result of the back result is: [111, 122, 133]
Map Instance One
Li = [1, 2, 3 = map (Lambda A, b:a + B, Li, sl)print(list ( new_list)) The result of the back result of the above instance running is: [12, 24, 36]
Map Example Two

Six, the filter function:

Filter the elements in a sequence, and finally get a sequence that matches the criteria

Li = [one, one, all= filter (lambda arg:arg >, li)print(list (new_list)) After running the above instance, the result of the reverse return is: [33]
Filter Instance

Python Learning Path The next day-function

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.