Python Learning Path five: Functional programming

Source: Internet
Author: User
Tags variable scope

This section focuses on

    • Mastering the function and grammar of functions

    • Mastering the knowledge of scopes, global variables and local variables

I. Basic knowledge of functional programming

1. Basic definition

A function is to encapsulate a set of statements by a name (function name), and to execute this function, you only need to invoke the name of the functor.

2. Features

? (1) Reduce duplication of code

? (2) Make the program extensible

? (3) Make the program easier to maintain

3. Syntax definitions

def Sayhi ():  #  function name    print("hello,i ' m nobody! ")
Two. Function parameters

1. Shape parametric

The memory unit is allocated only when it is called, and the allocated memory unit is freed at the end of the call. Therefore, the formal parameter is only valid inside the function. The parameter variable can no longer be used after the function call finishes returning the keynote function.

def Stu_register (name,age,course,country)     Pass

2. Actual parameters

Can be constants, variables, expressions, functions, and so on, regardless of the amount of the arguments, they must have a definite value when making a function call to pass these values to the formal parameter. Therefore, you should use the assignment, input and other methods to get the parameters to determine the value.

Stu_register (' song Jiang ', ' a ',' computer ','CN' )

3. Default parameters

? When defining a function parameter, you can give the parameter a default value, which is called the default parameter. Specifies that the default parameter must be after the position parameter.

def stu_register (name,age,course,country="CN")      Pass

4. Key parameters

? Normally, to pass parameters to the function in order, do not want to use the key parameters in order, just specify the parameter name (the argument name is specified as the key parameter), but remember a requirement is that the key parameters must be placed in the positional parameters (a positional order to determine the parameters of the corresponding relationship) after.

Stu_register (' song jiang ', 30,country='CN', course='  Computer ')

5. Non-fixed parameters

If your function is undefined, you can use non-fixed parameters when you are not sure how many arguments the user has passed.

def Stu_register (Name,age,*args):  #  *args incoming pass in tuple form    
def Stu_register (Name,age,*args,**kwargs):  #  *kwargs incoming pass in dictionary form     
Three. return value

? code outside the function to get the execution structure of the function, you can use the return statement in the function to return the result.

def stu_register (name,age,course='PY', country='CN'  ):    #  registration Code    if  registration successful:        return  True    Else:        return False

? You can use a tuple type when returning multiple values

def stu_register (name,age,course='PY', country='CN'  ):    #  registration Code    if  registration succeeds:        return  ( true,s_id)    Else:        return (False, Failure message)
Four. Global and local variables
    • A variable defined in a function is called a local variable, and a variable defined at the beginning of the program is called a global variable.

    • A global variable scope is the entire program, and the local variable scope is the function that defines the variable.

    • When a global variable has the same name as a local variable, the local variable functions within the function that defines the local variable, and in other places the global variable works.


Program design concept, usually, the name used in a program code is not always valid/available, but the name of the availability of the generation? The scope of this name is the range of the code.

Five. Nesting functions

Functions are defined in the body of another function, called nested functions.

Name ='Alex'defchange_name (): Name='Alex2'    defchange_name2 (): Name='Alex3'        Print("Third Layer Printing", name) change_name2 ()#calling the inner layer function    Print("Second Layer printing", name) change_name ()Print("Outermost print", name)
Six. Anonymous functions

? The anonymous function is the name of the specified letter that does not need to be displayed

Lambda x,y:x**y
Seven. higher-order functions

A variable can execute a function, and the argument of a function can accept a variable, and a function can accept another function as a parameter, which is called the higher order function.

? Just satisfy any one condition, immediate higher order function

    • Accept one or more functions as input

    • Return returns another function

def Add (x,y,f):     return f (x) += Add (3,-6, ABS)Print(res ())

Python Learning Path five: Functional programming

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: 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.