Functions of Python

Source: Internet
Author: User

What is a function? What role does it have?

The function is a whole, you can define your frequently used code as a function, when you go to call this function, it is equivalent to the entire function of the code inside, the code is the encapsulation code. Note that the function is a whole, so the inside variables are local variables, outside is not used, for example, I set a=5 in the function, outside the function we can not use this variable a

Define methods and call methods as follows:

Def Zhouyu (): #定义一个zhouyu的函数 print ("I am Zhouyu") #zhouyu函数内的封装代码zhouyu () #调用zhouyu这个函数

Execution results

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/A5/70/wKioL1m90U6jOd-CAAAdStJVpBI940.png "title=" _ 20170917093442.png "alt=" Wkiol1m90u6jod-caaadstjvpbi940.png "/>


This is a function without parameters, in general, the function may have some number is changed, this number is the parameter, the number can be the result of the above code, or the user input values into the function, in place of this parameter to make the function more usable. Like what:

def sum_2_nums (b): sum1 = a + b print ("%d +%d =%d"% (A,B,SUM1)) sum_2_nums (100,200)

As can be seen from the above code, we define a function sum_2_nums, the function has two parameters, one is a, the other is B,(note: Parameters are usually written in the parentheses of the function) These two parameters can be in the enclosing code should be, As the second line of the above code can know that the two parameters are applied, the following we see the fourth line of code, you can see that the function is called, and the difference is called before the function is called, but also added two numbers, one is 100, one is 200, actually can be this, When we call this function, the code will go to see if there is no such function, if there is, will execute this function, it is clear that there is this function, and this function requires us to provide two parameters, A and a B, then we call this function, we have to provide two parameters, such as the Code, The parameters we provide are the 100 and 200 functions that will put a=100,b=200 into this function code to execute, and the following results will appear:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A5/71/wKioL1m92BLDjy_gAAADfcwDbXo983.png "title=" _ 20170917093442.png "alt=" Wkiol1m92bldjy_gaaadfcwdbxo983.png "/>

Like the above situation, A, B is the formal parameter, and 100,200 is the actual parameter, the formal parameter is actually an image of the parameters, is virtual, and the real parameter is the real parameters, such as 100,200 is the actual parameters.

If you replace 100,200 of the above code with something else, the values of A and B will change as well.

Here's the function of return.

Return is the result of the execution of a method, such as the following we want to get sum1 results, can be obtained by return

def sum_function (b): sum1 = a + b return Sum1print (Sum_function (100,200))

If you do not join return sum1, you go to execute this code, sum_function (100,200) The result of this method is empty, that is, none, there is nothing to print out, because this function does not return any data. There is, of course, another way to define this sum1 as a global variable that can be used in the following code:

def new_sum (number): global sum2 sum2 = number + 3new_sum () print (SUM2)

Execution results are

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A5/75/wKioL1m-HlqyOggsAABOBvP1gBY767.png "title=" _ 20170917093442.png "alt=" Wkiol1m-hlqyoggsaabobvp1gby767.png "/>

Global is actually declaring a global variable so that we can go outside with this variable.

Some classmates may say, then I want its return value to do, see below, if New_sum want to get the result of the previous method as a variable of my method, what to do, because the variable inside the function is a local variable, is not obtained outside, so we can get what we want by the return value:

def sum_function (b): sum1 = a + b return sum1def new_sum (number): sum2 = number + 3 print (sum2) A = sum_funct Ion (100,200) New_sum (a)

When the above code executes to the 7th line of code, it calls the Sum_function function and passes in two values, 100 and 200. This is the function that executes sum_function, because there is a return in this function, So we will go to return the value assigned to a, so that a has value, and then the value of a is assigned to the New_sum function

Note that the return value can be multiple, but return can only have one, because the method at the time of execution, see return it does not execute the following code, the direct exit method, then if we want to return more than the value of what to do, the return value can be any type, such as tuples, dictionaries, lists. That way, we can put multiple values in a dictionary or a tuple or a list.

The code is as follows:

def sum_function (b): sum1 = a + b d = {A,B,SUM1} return da = Sum_function (100,200) print (a) print (type (a))

So what we're returning is a set data type.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/A5/72/wKioL1m97t6SyTHVAABdh5CQfgw963.png "title=" _ 20170917093442.png "alt=" Wkiol1m97t6sythvaabdh5cqfgw963.png "/>

If you want to return a list, you can use the following method code as follows:

def sum_function (b): sum1 = a + b return {a,b,sum1}a = Sum_function (100,200) print (a) print (type (a))

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/06/C2/wKiom1m97-OxhFqHAABOrUdqGgc884.png "title=" _ 20170917093442.png "alt=" Wkiom1m97-oxhfqhaaborudqggc884.png "/>

The same is true of dictionaries

If you return more than one value by default, the method encapsulates it into a tuple with the following code:

def sum_function (b): sum1 = a + b return A,b,sum1print (Sum_function (100,200))

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/06/C2/wKiom1m98ZfDq6IkAAA8BR5TGwc092.png "title=" _ 20170917093442.png "alt=" Wkiom1m98zfdq6ikaaa8br5tgwc092.png "/>

Here's a look at the nesting of functions

The following code is to nest the Print_line function on the Print_5_line function

Def print_line (): Print ("-" *50) def print_5_line (): i = 1 while I <= 5:print_line () i = i + 1pri Nt_5_line ()

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A5/73/wKioL1m9_Y-AwvD4AABsErVGNm4474.png "title=" _ 20170917093442.png "alt=" Wkiol1m9_y-awvd4aabservgnm4474.png "/>

The above results can show that functions can be nested.

Here is the default parameter, the default parameter is the parameter has a default value, if you do not give this parameter value, this parameter will use its default value. in the following code, the Zhouyu function has two parameters A and B, that is, when we go to call it to pass two arguments, then why the code to call Zhouyu when only passing a parameter but no error, because B=12 is the default parameter, that is, if only one parameter is passed in, Then B will use the default value, which is 12, so the execution result is sum = 13.

def Zhouyu (a,b=12): Sum1 = a + b print ("sum =%d"%sum1) Zhouyu (1)

If you call Zhouyu to pass in two parameters, then the b=12 will not take effect, as follows:

def Zhouyu (a,b=12): Sum1 = a + b print ("sum =%d"%sum1) Zhouyu (1,22)

Execution result is sum = 23

Note here that the default parameters can only be placed at the end, not in the middle or in front, such as the following code, will be error:

def Zhouyu (a=12,b): Sum1 = a + b print ("sum =%d"%sum1) Zhouyu (1,22)

If you have three parameters and the last two are the default parameters, and then you just want to let the third parameter not use the default value, with the value you passed in, what to do, you can look at the following code:

def Zhouyu (a,b=12,c=22): Sum1 = a + B + C print ("sum =%d"%sum1) Zhouyu (1,c=33)

From the above code can see that the default value of B is 12,c default value is 22, if you want to change the value of C without changing the value of B, you can pass in the parameter when the c=33, so that C will not apply the default value of 22, and with a, like this we call c=33 is named parameter

Here we know exactly what we want to pass in a few parameters, so what if we don't know what we want to pass in a few parameters?

def long_argue (a,b,*argue): print (a) print (b) print (argue) long_argue (1,2,3,4)

In the above code, *argue is used to receive extra arguments, we can look at the results of the execution

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A5/77/wKioL1m-TxSDXsaUAABeebFGZP4264.png "title=" _ 20170917093442.png "alt=" Wkiol1m-txsdxsauaabeebfgzp4264.png "/>

The above code, the extra 3,4 into the argue this parameter into a tuple, then it is what use, for example, we want to let a lot of values added, but do not know how many values, it can be achieved through this. The following code:

def long_argue (A,b,*argue): For i in argue:sum1 = sum1 + I sum1 = sum1 + A + blong_argue (1,2,3,4)

From the above code can be known, the extra 3,4 passed to the argue, so that argue is a tuple, if you want to let them want to add, you have to put the value of the tuple in one of the out, and let them add, and then let the result to add a, a, and so you can achieve a number of values added.

Here is another, that is, after *argue add **kwargue This parameter, then what does it mean, you can see the following code explanation:

def long_argue (a,b,*argue,**kwargue): print (a) print (b) print (argue) print (Kwargue) long_argue (1,2,3,4)

The results of the implementation are as follows:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/A5/77/wKioL1m-UUOhrVuPAABj8j_fMx4115.png "title=" _ 20170917093442.png "alt=" Wkiol1m-uuohrvupaabj8j_fmx4115.png "/>

As you can see, our *argue is used to receive some numeric values or strings, and the data is converted into tuples, and **argue is used to receive named parameters (what are named parameters can look at the Red font section above, there are related explanations) and turn these named parameters into dictionaries.

If we have a tuple and a dictionary, and now want to pass the tuple and the dictionary to the function, what should we do, we can look at the following code:

def long_argue (a,b,*argue,**kwargue): print (a) print (b) print (argue) print (kwargue) a = (44,55,66) b = {' Name ': ' Zhouyu ', ' Age ': 22}long_argue (11,22,*a,**b)

If you are not in the 8th line of code, A and B preceded by the * number, the pass-through parameter will default to A and B as a whole, that is, the string, and then the two strings are passed into the *argue parameter, it becomes the value of the tuple. And the above code is to split the *a into a number, that is, the value of 44,55,66, and then passed to *argue, the B split into "name" = "Zhouyu", "age" = 22, and then passed in B, like this process, we call this a unpacking


This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1966099

Functions of Python

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.