Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create python function

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to create python function". In daily operation, I believe many people have doubts about how to create python function. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to create python function". Next, please follow the editor to study!

I. handling of documents

Have a certain understanding of data storage and file processing when learning crawlers

There is no more to say here, links: data storage (1), data storage (2)

Master the steps of file processing is easy to understand, for other documents do not understand the processing (database) and so on, you can first understand open ().

Second, function

Overview of functions:

Write a function well, we give it a name, convenient for us to use next time, reduce the repetition of code.

1. Create a function and calculate 1 / 2 and output

Def add ():

X = 1x 2

Print (x)

Add ()

Def declares a function

Add, you declare the name of a function

Add () calls this function

2. Return value

According to the example in 1, if we don't want to print the value of x, we need to use this x next. I just want to get the value of this x. The return value of the function is used. We can change the function in 1 to a function with a return value.

Def add ():

X = 1x 2

Return x

Print (add ())

Return: return value, that is, x. It is worth noting that return represents the end of a function. The following code is no longer executed.

Add (): at this point, when we call add (), we will not output the value of x, and add () represents the value of x. If we want to output, we can output add ().

Any more questions?

If we don't return a value in 1, what does the output add () return?

Can I return multiple values?

You can try it yourself!

3. Parameters

To think of a problem, we add this function is only a fixed calculation of 1: 2, what if we want to calculate the value entered by the user? The parameters are used here.

3.1 position parameters (must input values, one by one corresponding, more, less will report an error. )

A = int (input ("enter a:"))

B = int (input ("enter b:"))

Def add (XBI y):

Return Xeroy

C = add (aPerm b)

Print (c)

Add (a _ force b): pass a _ line b to the add () function, where a corresponds to x _ force b and y to y. XQuery y is the formal parameter, and aPerry d is the actual parameter. There is an one-to-one correspondence between a _ r _ r _ b and x _ ray _ y.

In addition: we can also pass parameters in this way add (1Magne2) location parameters, can also pass parameters add keyword (xtrem1djinyqb) keyword, can also be mixed use, but the position parameters first, keyword parameters later. Add is not allowed.

3.2 default parameters (transferable, but not transferable)

Default parameter: when there is a value passed in, we use the passed value, and when no value is passed in, we can use the default value.

Note: the default parameter must be after the position parameter.

A = int (input ("enter a:"))

B = int (input ("enter b:"))

Def add (xQuery yearly 10):

Return Xeroy

C = add (a)

Print (c)

D = add (aPerm b)

Print (d)

The default parameter in add (xquotation 10): 10 is the default parameter. When two parameters are passed in, the default value is not used. When only one parameter is passed in, the default value, 10, is passed in the second parameter.

3.3 dynamic parameters

Dynamic parameters: we can use dynamic parameters when we are not sure how many parameters the function needs to pass, such as calculating the sum of multiple numbers.

Def add (* args):

Sum = 0

For i in args:

Sum + = I

Return sum

Add (1, 2, 3)

Add (1, 1, 1, 1, 1)

* args: after adding a * sign before the parameter name, you can pass any number of parameters. The name args can be changed, and there is no problem, but it is equivalent to everyone writing it in this way, so you can see at a glance that the position of the dynamic parameter needs to be placed in front of the default parameter. This sentence does not mean that there must be a default parameter, but in front of the default parameter if necessary.

Def add (* * kwargs):

Print (kwargs)

Add (axiom 1 ~ 2 ~ 3)

Add (axi1)

* * kwargs: plus two * passed in the form of keywords, as in our dictionary, a dictionary output is a dictionary.

Note: if both are needed, * args needs to be placed in front of * * kwargs

Summary: the order in which parameters are passed: position parameter, * args, default parameter, * * kwargs, without any of these parameters, the same order. When there are dynamic parameters, the default parameters must be passed in the form of keywords.

3.4 dynamically pass lists and dictionaries.

List = [1pm 2pm 3]

Def add (* args):

Print (args)

If we add (list) directly, then what we output is also a list, that is, without separation.

If we want to achieve the effect of add (1 list 2), all we need is this: add (* list)

Similarly: there is a dictionary dict= {"name": "wmm", "sex": "nan"}

Only add (* * dict) is required.

Another: there are usually people who call a function a method, and a function and a method are the same thing, but we usually call a built-in function a function and a custom method, which doesn't matter.

Misunderstanding:

This misunderstanding has been seen in teacher Liao Xuefeng's blog. I think it is very interesting. I would like to share it with you here.

Def add (list= [])

List.append (1)

Print (list)

Add () # result: [1]

Add () # result: [1d1]

Add () # result: [1, 1, 1, 1]

We found that each call will be added to the original list, and the effect we want is only added to [1]

We can write like this:

Def add (list= [])

If list! = None:

List = []

List.append (1)

Print (list)

That's it.

Global variables and local variables

X = 1

Def fun ():

Print (x)

Where x is a global variable, you can refer to it in the function

Def fun ():

Xero1

Print (x)

Error report: here x is a local variable and cannot be referenced in the global, so an error is reported.

X = 1

Def fun ():

Xerox 10

Print (x)

Error report: if we want to change the value of a global variable in a function, we need to declare it to be a global variable with global.

That is, it is modified to:

X = 1

Def fun ():

Global x

Xerox 10

Print (x)

At this point, the study of "how to create python functions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report