Python basic grammar learning - function


foreword

Continuing with this week's study,


1. Function

The function definition may contain multiple formal parameters, so the function call may also contain multiple actual parameters. There are many ways to pass actual parameters to the function: positional actual parameters can be used, and keyword actual parameters can also be used.

1. Positional arguments

Based on the order of the actual parameters, associate them to the formal parameters.

def describe_pet(animal_type,pet_name):
    print("I have a {animal_type}.".format(animal_type = animal_type))
    print("My {animal_type} name is {pet_name}".format(animal_type = animal_type,pet_name = pet_name))

    
describe_pet('harry','hamster')

In this example, the first actual parameter 'harry'is passed to animal_typethe formal parameter.

2. Keyword arguments

describe_pet(animal_type = 'harry',pet_name = 'hamster')

It clearly indicates which formal parameter to pass the actual parameter to. The order is irrelevant.

3. Default value

You can assign a default value to a formal parameter when defining a function, and it will be the default value if you do not assign a value to the formal parameter when calling it. When defining, the formal parameters with default values ​​must be written after the formal parameters without default values, in order to still be able to interpret the positional arguments correctly.

4. Make arguments optional

Sometimes we may or may not need a formal parameter in a function. When calling, in order to ensure that the function can run normally, set the default value of this formal parameter to the end, so that when ‘ ’calling , if there is no such actual parameter, its default value is empty, if there is this actual parameter, specify its value.

5. Pass the list to the function

If we pass a list to the function and modify the list in the function, the list of actual parameters will also be modified (understood as passing the address of the list) for
example

a = [1,2,3,4]
def list_a(a):
    a.pop()

list_a(a)
a
[1, 2, 3]

It can be seen that the last element of the list a is deleted in the function. After the function is called, the list a does change outside the function.
If we want to change the list only inside the function, but the actual list a does not change, how should we do it?
A copy of the list a can be passed in. As follows

a = [1,2,3,4]
list_a(a[:])
a
[1, 2, 3, 4]

6. Pass any number of arguments

If you don't know how many parameters the function needs to accept, you can define only one formal parameter when defining the function.
For example

def deng(*x):
    print(x)

deng(1)
(1,)
deng(1,2,3,4)
(1, 2, 3, 4)

As you can see, add a number before the formal parameter *, which can accept any number of actual parameters, and convert all actual parameters into a tuple.

Note that if there are other positional or keyword arguments, we need to *argsplace them after the other formal parameters.

7. Pass any number of keyword arguments

If we want to accept any number of arguments, but don't know what kind of information is passed to the function. Functions can be written to accept any number of key-value pairs.
Examples are as follows:

def build_profile(first_name,last_name,**user_name):
    user_name['first_name'] = first_name
    user_name['last_name'] = last_name
    return user_name

user_profile = build_profile('deng','chao',location = 'princeton',field = 'physics')

print(user_profile)
{
    
    'location': 'princeton', 'field': 'physics', 'first_name': 'deng', 'last_name': 'chao'}

When defining the function, I added two before the formal parameter user_name **. The function of this is to store the subsequent parameters in the dictionary user_name in the form of key-value pairs, and this dictionary can also be accessed inside the function. user_name.

Guess you like

Origin blog.csdn.net/weixin_47250738/article/details/130598439