Python basics: do you really understand functions?

Hello, here is GUIDM. This article is about python basic functions. I hope this content can be helpful to you.

function - a code segment.
Single function reusability
Create a function with three elements:
1. Function name
naming convention : as the name implies, camel case (big camel [when defining a class name] ClassName, small camel getName), underscore. Identifier -**In addition to keywords, English, numbers, underscores, and unicode characters are commonly used in China, Japan, and Korea.
2. Parameters - formal parameters, actual parameters
3. Return value

After the function is written, it needs to be called

Format:
def function name (incoming parameters):

​ Function body

​ return return value

Define the function first, then call the function.

Parameters and return values ​​are not required and can be omitted.

def getSum() :
    a = 1
    b = 5
    sum = a+b
    print('a+b=', sum)
getSum() #函数名可以理解为变量
def getSumb(a,b): #带固定参数无返回值
    sum1 = a+b
    print("a+b=",sum1)
getSumb(1,5)
def getSum(a,b):#带参有返回值
    sum = a+b
    return sum
s = getSum(1,5) #有返回值的函数一定要拿到返回值,保存到一个变量中。

return directly ends the call of the function

Automatically destroy and recycle resources.

def loginIn(user_name,password):
    if user_name == 'admin' and password == '123456':
        #print('登陆成功!')
        return user_name, password#以元组的形式返回
    else:
        #print('登陆失败!')
        return 1
g = loginIn('admin','123456')
print(g)
def getMax(a, b):
    num_max = max(a, b)
    return num_max
max = getMax(2, 4)
print('最大数为:{}'.format(max))

variable role

1. Global variables

Is it possible to modify the value in the parameter body? - address, value

The type of the parameter depends on:

  1. The value changes and the address also changes: int float str tuple bool
  2. The value changes and the address remains unchanged: list (ordered, repeatable) dict set (unordered, non-repeated)

2. Local variables: defined in the function body, global

Local variables are destroyed when the function body is destroyed.

3. The problem of the same name - priority access to local variables in the polymorphic function body.

Parameter transfer: in c, the transfer method can be specified (value transfer, address transfer)

​ In python, the address is passed.

id() : Returns the memory address of the object.

def getSum(a,b):
    a = 10
    print(id(a))
    print('函数体内局部变量a的内存地址:', id(a))
    sum = a+b
    return sum
a = 100
print('全局变量a的内存地址:',id(a))
b = 300
print('a+b=', getSum(a, b))

parameter:

1. Required parameters: number, order

2. Keyword parameters: Out of order

3. Default parameters: placed behind all parameter lists

4. Variable length parameter

1) An asterisk – tuple type

​ 2) Two models – dictionary type

def getSum(a,b,c,*d)#加一个*号的变量存放的是未命名的参数,元组的格式。

abs(): Python built-in function for absolute value.

Function with multiple return values
def test_return():
    return 1, 2
x,y = test_retrun()

Variables are separated by commas, different types of data returns are supported, and data is received in order.

Multiple ways of passing parameters to functions
  1. Positional parameters: When calling a function, parameters are passed according to the parameter positions defined by the function. The order and number of passed parameters and defined parameters must be consistent.
  2. Keyword parameters: When a function is called, passing parameters in the form of "key = value" can make the function clearer and easier to use, and also remove the order requirements of the parameters. If there are positional parameters when the function is called, the positional parameters must be in front of the keyword parameters, but there is no order between the keyword parameters.
  3. Default parameter: You can set a default value when passing a parameter, and you can not pass the value of the parameter when calling the function, and the default parameter must be written at the end.
  4. Variable length parameters: also called variable parameters, used to determine how many parameters will be passed when calling. When the number of parameters is uncertain when calling a function, you can use variable length parameters.
anonymous function

The def keyword can define a function with a name, and the lambda keyword can define an anonymous function (without a name).

An anonymous function without a name that can only be used temporarily once.

lambda 传入参数 :函数体(一行代码)

Guess you like

Origin blog.csdn.net/m0_61901625/article/details/131261072