Python Basics-Day 2 learning note-function function

Source: Internet
Author: User

Objective:

Three Ways to Program

    1. Object-Oriented: Classes (Class)
    2. Object-oriented: Process (Def)
    3. Functional Programming: function (Def)
Definition: function vs. process

In Python, a function is a programmatic way to structure or process program logic. Functional programming is the definition of a mathematical function, which is then implemented in a programming language using this mathematical model.

procedural programming is functional programming with no return value . See the following

def func1 ():     ' The function discption-define a function '   # documentation, highly recommended for interpreting function logic    Print ('in thefunc1')     return 0x=func1 ()print('fromfunc1 return is%s' %x)

In the FUNC1
From Func1 return is 0

Process- oriented : Define a procedure that is a function that has no return value

def Func2 ():     ' Define a process '    Print ('in thefunc2'# defines a procedure, which is a function that does not return a value y= Func2 () Print ('fromfunc2 return is%s' %y)

In the Func2
From Func2 return is None

return value of the function
The number of objects returned Objects that are actually returned by Python
0 None
1 Object
(including a list, or another function)
>1 Tuple
    • Set the reason for the return value: The result of the entire execution of the function is required, and the subsequent program needs to operate according to the execution result.
Role of the function:

1, reusable 2, to keep the code consistent 3, scalability

Calling Function-parameters

1. Keyword invocation (independent of formal parameter order) and standard position call (corresponding to parameter one by one): Key parameter cannot be written in front of position parameter, and is called as position in mixed use.

def Test (x, y):  #  form parameter, is a dummy variable.     Print(x)    print(y) Test (#)  #  Standard Location Call method  #  keyword call

2. Default parameter: Call function city, default function is optional, non-mandatory. The purpose is primarily to default installation values and to contact the port number of the database.

3. Parameter groups: Pass a tuple * (non-keyword argument) or dictionary * * (keyword parameter) as a parameter group to the function. The parameter group must be put last.

def func (*args):    print(args) func(1,3,4,3,67) func (

(1, 3, 4, 3, 67)

(1, 3, 4, 3, 67)

def func1 (* *Kwargs)    :print(Kwargs) func1 (name='Alex  ', age='8', sex='F')  # Must be in the form of a keyword argument

{' name ': ' Alex ', ' age ': ' 8 ', ' Sex ': ' F '}

Variables scoped to variables defined in a subroutine are calledLocal Variables, a variable defined at the beginning of the program is calledGlobal Variables。 The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable. When a global variable has the same name as a local variable: Local variables work within subroutines that define local variables, and global variables work in other places. Recursively within a function, you can call other functions. If a function calls itself internally, the function is a recursive function. Recursive characteristics 1. There must be a definite end condition of 2. Each time you enter a deeper level of recursion, the problem size should be reduced by 3 compared to the last recursion. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, whenever entering a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow.* Stack Literacy http://www.cnblogs.com/lln7777/archive/2012/03/14/2396164.html
def Calc (n):     Print (n)     if int (N/2) >0:        return calc (int (n/2))    print("  , N) Calc   #  recursion is up to 999 times

Function-Type 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.

Python provides partial support for functional programming. Because Python allows the use of variables, Python is not a purely functional programming language. Functional programming language has Erlang,hashhell and so on. Lambda expressions and four other built-in functions. (to be added)

    • anonymous functions and lambda
    • Built-in functions-apply ()
    • Built-in functions-filter ()
    • Built-in functions-map ()
    • Built-in functions-reduce ()

Higher order functions

Python Basics-Day 2 learning note-function 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.