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

Source: Internet
Author: User

1. What is the basic syntax and function of the function?

function is derived from mathematics, but the concept of "function" in programming is very different from the function in mathematics, the specific difference, we will say later, the function of programming in English also has a lot of different names. In basic it is called subroutine (sub-process or subroutine), in Pascal is called procedure (process) and function, in C only function, in Java is called method.

Definition: A function that encapsulates a set of statements by a name (function name), and to execute the function, simply call the name of its functions

Characteristics:

    1. Reduce duplicate code
    2. To make the program extensible
    3. Make programs easier to maintain
Syntax definitions
Def sayhi (): #函数名    print ("Hello, I ' m nobody!") Sayhi () #调用函数

can take parameters

#下面这段代码a, b = 5,8c = A**bprint (c)  #改成用函数写def calc (x, y):    res = x**y    return res #返回函数执行结果 c = Calc (b) # result assigned to c variable Print (c)

  

2. Function parameters and Local variables

A parameter variable allocates a memory unit only when it is called, releasing the allocated memory unit immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. Function call ends when you return to the keynote function, you can no longer use the shape parametric

arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter. It is therefore necessary to use the assignment, input and other methods to get the parameters to determine the value

Default parameters

Look at the code below

def stu_register (name,age,country,course): Print ("    ----Enroll Student information------")    print ("Name:", name) print ("Age    :" , age)    print ("Nationality:", country)    print ("Course:", course) Stu_register ("King Shanbao", "CN", "Python_devops") stu_register ("Zhang Jiaochun", (+), "cn", "Linux") Stu_register ("Liu Lao Root", "cn", "GB", "Linux")

Found country This parameter is basically "CN", just like we registered users on the site, such as the nationality of this information, you do not fill in, the default will be China, which is implemented by default parameters, the country into the default parameters is very simple

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

Thus, this parameter is not specified at the time of invocation, and the default is CN, specified by the value you specify.

In addition, you may have noticed that after turning the country into the default parameter, I moved the position to the last side, why?

Key parameters

Under normal circumstances, to pass parameters to the function in order, do not want to order the key parameters can be used, just specify the parameter name, But remember a requirement is that the key parameters must be placed after the position parameter.

Stu_register (age=22,name= ' Alex ', course= "Python",)

Non-fixed parameters

If your function is not sure how many parameters the user wants to pass in the definition, you can use the non-fixed parameter

def stu_register (Name,age,*args): # *args will turn multiple incoming parameters into a tuple form    print (Name,age,args) stu_register ("Alex") #输出 #alex () #后面这个 () is args, just because there is no value, so the empty Stu_register ("Jack", "+", "cn", "Python") #输出 # jack (' cn ', ' Python ')

can also have a **kwargs

def stu_register (Name,age,*args,**kwargs): # *kwargs will turn multiple incoming parameters into a dict form    print (Name,age,args,kwargs) stu_ Register ("Alex", "#alex") #输出 () {} #后面这个 {} is Kwargs, just because there is no value, so the empty Stu_register ("Jack", "+", "CN", "Python", sex= "Male", Province= "Shandong") #输出 # Jack (' CN ', ' Python ') {' Province ': ' Shandong ', ' sex ': ' Male '}

 

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

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