Day3-python Basic 3 functions, recursion, built-in functions

Source: Internet
Author: User

Day3-python Basic 3 function, recursive, built-in function 1. Basic syntax and characteristics of functions

definition : Encapsulates a set of statements by a name (function name), which is called only when the function is executed.

 features :

1. Avoid duplicate code

2. Enhanced Program Extensibility

3. Easy to maintain code

2. Parameters and Local variables

The parameter variable is allocated only when it is called, and the memory unit is freed immediately after the call ends. Therefore, the formal parameter is only valid inside the function, and when the function call returns the function of the keynote, the argument's variable is no longer available

  argument variables can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when a function is called, it must be a definite value when passed to the parameter. Therefore, the method of assignment, input, etc. should be used beforehand to obtain the definite value of the formal parameter.

Local variables: variables defined within a function that work only when the function is run and automatically released after the run is finished

Default parameters:
def stuffs (name,age,country='CN'):    print (" name:%s" %name)    print (" Age:%d" % ages )    print ("  Country:%s" % Country) stuffs ('sam',18 )
non-fixed parameters:
#*args will change the multi-pass parameter into a tuple.>>>defStuffs (name,age,*args): ...Print(Name,age,args) ...>>> Stuffs ('Sam', 18,'Football','Movie','Python') Sam18 ('Football','Movie','Python')>>>#**kwargs will change the parameters of the multi-pass into a dictionary.>>>defStuffs (name,age,*args,**Kwargs): ...Print(Name,age,args,kwargs) ...>>> Stuffs ('Sam', 18,'CN','python', sex='Mans', hobby='Running') Sam18 ('CN','python') {'Hobby':'Running','Sex':'Mans'}>>>
  Key parameters:

 The key parameter must be after the position parameter  

>>>defStuffs (name,age,country='CN'):...     Print("Name:%s"%name) ...Print("Age:%d"%Age ) ...Print("Country:%s"%country) ...>>>>>> Stuffs (country='USA','Sam', 18)#key parameters in front of the parameter, resulting in an errorFile"<stdin>", Line 1syntaxerror:positional argument follows keyword argument>>> Stuffs ('Sam', 18,country='USA')#the parameters of the key parameter are normal after the bit parameterName: Sam Age:18Country: USA>>>
  Global and local variables:

A variable defined in a subroutine is called a local variable, and a variable defined at the beginning of a program is called a global variable.

A local variable scope is a subroutine that defines the variable, and the scope of the global variable is the entire program

Local variables work in sub-programs that define local variables when they have the same name, and other local global variables work

3. Return value

To get the execution result, you can return the result with return

    • As soon as the function executes, it stops executing and returns the result whenever a return statement is encountered, which can be understood as return is the end of the function.
    • function if return is not set, the default return value is None
Nested functions

In-function re-apply function

Name ='Sam'defchange_name (): Name='sam2'    defchange_name2 (): Name='SAM3'        Print("3rd Layer Printing", name) change_name2 ()#calling the inner layer function    Print('2nd Layer Printing', name) change_name ()Print('1th Layer Printing', name)
4. Recursion

If a function calls itself internally, this is called a recursive function

Characteristics:

1. To have a definite end condition

2. Each time a deep level of recursion is reached, the size of the problem is reduced relative to the previous

3. Recursive efficiency is not high, too many recursive levels can lead to stack overflow

5. Anonymous functions

Do not need to display the specified function, and use with other functions

>>> calc = Lambda n:n**n

>>> Print(calc)

10000000000

#和其它函数配合使用
>>> res = map (lambda n:n**3,[1,2,3,4])>>> res for in res: ... Print (i) ... 182764
6. Introduction to Functional programming

function is a kind of encapsulation supported by Python, we can decompose complex tasks into simple tasks by splitting large pieces of code into functions through a layer of function calls, which can be called process-oriented programming. function is the basic unit of process-oriented program design.

Functions in functional programming the term does not refer to a function in a computer (actually a subroutine), but rather to a function in mathematics, the mapping of an independent variable. This means that the value of a function is determined only by the value of the function parameter, not by other states. For example, the sqrt (x) function calculates the square root of X, as long as x is not changed, and whenever the call is called, the value is constant.

First, the definition

Simply put, "functional programming" is a "programming paradigm" (programming paradigm), which is how to write a program's methodology. The main idea is to write the operation process as much as possible into a series of nested function calls.

7. Higher-order functions

A variable can point to a function, which can accept a variable, and a function can accept another function as a parameter, which is called a higher order function.

1 def Add (x,y,f): 2 ...     return f (x) + f (y)3 ... 4 >>> res = Add (3,-6, ABS)5print(res)6 9
8. Built-in functions

Built-in Parameter details Https://docs.python.org/3/library/functions.html?highlight=built#ascii

Day3-python Basic 3 functions, recursion, built-in 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.