Detailed Explanation and Application Example of python Function

  • 2021-12-12 08:52:42
  • OfStack

How to define a directory How to use a function What are parameters Indefinite length parameters What are return

How to define a function

Function is also called method. Write a simple function below:


def function():
	print(" I'm a function. ")

This is a very simple function. The function only does one thing and prints out "I am a function." This sentence.
Viewed from the outside of this function, there is a def , and there's another one function Add a () And : .
def The keywords that define the function cannot be changed. function Is the name of the function, you can write it casually, and then add it () And : This is the rule.
The output statement inside the function can be called the function body. All function bodies need to be indented, otherwise that will report an error.

How to use

The way to use it is very simple


function()

Repeat the function name with parentheses, and you can use it

What are parameters

Next, I write a function:


def function(a,b):
	print(a,b)

In this function, a and b in parentheses are called parameters (function parameters) and formal parameters. What do parameters do? The function of the parameter is to pass 1 something into this objective function.

For example, in this function, you can print the parameters passed in from outside:


function("aaa","bbb")

I passed two parameters to this function. The position of the parameters cannot be changed. "aaa" can be the actual parameter, corresponding to the formal parameter a, and "bbb" corresponding to b. This passed-in procedure is 参数传递

The order of the result output is:

aaa bbb

You can also pass parameters as follows:


function(a="aaa","bbb")
function("aaa",b="bbb")
function(a="aaa",b="bbb")
function(b="bbb",a="aaa")

When there are parameters in the function we define, when using it, 1 must pass in parameters otherwise it will report errors, such as:


def function(a):
	print(a)

function()

Run results:

TypeError Traceback (most recent call last)
< ipython-input-1-1ea14deaa8c8 > in < module >
2 print(a)
3
---- > 4 function()

TypeError: function() missing 1 required positional argument: 'a'

Indefinite length parameter

What should I do if I don't know how many parameters are passed?
Can be used * And function0 To achieve.

Add * Parameters of are passed in as tuples (tuple) to hold all unnamed variable parameters, such as:


def function(*args):
	print(args)
function(12,35,65)

Run results:

(12, 35, 65)

It is found that the output result is 1 tuple containing all the passed parameters

Added two asterisks function0 Parameters of are passed in the form of dictionaries;


def function(**kwargs):
	print(kwargs)
function(a=12,b=35,c=65)

Note that the parameters passed in here are key-value pairs.

When one asterisk and two asterisks occur at the same time, one asterisk must precede the two asterisks, as listed in:


def function(*args, **kwargs):
	print(args)
	print(kwargs)

What is return

If return appears in the function, it means that the function is finished here, and no matter how many it is, it will not be executed again. And return returns the value of the expression after it, which is equivalent to assigning the following value to the function, for example:


def function():
	print("aa")
	return "aaa"
	print("bb")

print(function())

Run results:

aa
aaa

Discover return The following statement exits without executing the function, and the function itself has a value.


Related articles: