Function a function acquaintance

First, the function acquaintance

  1. Function acquaintance

    1.1 Object-oriented programming

    Code duplication

    Code readability is not high

    获取列表的元素的个数
    l1 = [1, 2, 3]
    count = 0
    for i in l1:
        count += 1
    print(count)

    1.2 Functions

    It is a function-oriented function, a function of a function package. Reducing repetitive code to enhance the readability of the code.

    li = ['1','a','ad','sfe','sef']
    def new_len():
        count = 0
        for i in li:
            count += 1
        print(count)
    new_len()
  2. Structure Function

    def Keywords: define a function, followed by a space

    new_len function name: consistent with variable naming, it must be provided with descriptive (implemented to reflect the performance function)

    (): Structure requires, mass participation used: delimiter

  3. Call functions

    That execution of the function, def new_len () is only defined function, expressed in current step defines a function, but as long as the statement is not performed this function will not be executed.

    new_len () function name + () function as a performer, also known as the caller to complete the call to the function to function.

    new_len()
    new_len()
    new_len()
    结果:
    5
    5
    5

    How many times a function to perform the function of writing is executed many times.

  4. Function's return value

    The function return statement is used to indicate the end of function execution and returns to the calling function execution result of the function.

    After the return with no value, mean the function terminates, the end of the function, the default return value None

    No function return, the return value is None

    def new_len():
        count = 0
        for i in li:
            count += 1
    print(new_len())
    结果:
    None

    followed by a single return value of the function is performed by the value obtained

    def func():
        print(123)
        return 321
    print(func())
    结果:
    123
    321

    A plurality of rear return value of the function is obtained by the implementation of a tuple

    def func():
        print(123)
        return 1,"a",[1,2,3]
    print(func())
    结果:
    123
    (1, 'a', [1, 2, 3])
  5. Function parameter passing

    1.1 function parameter passing can function with scalability, function is function-oriented, wrote in the function code is dead, but can not pass argument that it becomes like a variable assigned different values ​​in different result.

    def new_len(a):  #定义函数时:参数:形参。
        count = 0
        for i in a:
            count += 1
        return count
    li = ['1', 234, 1]
    print(new_len(li))# 函数的调用者:参数 实参。
    结果:
    3

    1.2 argument

    Parameter in the function calling part called arguments, parameters can be divided into location, keyword parameters, the mixing parameter

    Location parameters: left to right, in order, one-

    def meet(sex,age,job):
        print('左划一下')
        print('右划一下')
        print(f'寻找性别{sex},年龄{age}岁,{job}')
        print('聊天')
        print('约吗')
        print('约...')
    meet('女','18-25','讲师')
    结果:
    左划一下
    右划一下
    寻找性别女,年龄18-25岁,讲师
    聊天
    约吗
    约...

    Keyword arguments: one to one

    def meet(sex,age,job,hight,weight):
        print('左划一下')
        print('右划一下')
        print(f'寻找性别{sex},年龄{age}岁,工作{job},身高{hight},体重{weight}')
        print('聊天')
        print('约吗')
        print('约...')
    meet(sex='女',age='18-25',job='学生',hight='170',weight='120')
    结果:
    左划一下
    右划一下
    寻找性别女,年龄18-25岁,工作学生,身高170,体重120
    聊天
    约吗
    约...

    Mixing parameters: Keyword parameters must be in the rear position parameters, one by one.

    def meet(sex,age,job,hight,weight):
        print('左划一下')
        print('右划一下')
        print(f'寻找性别{sex},年龄{age}岁,工作{job},身高{hight},体重{weight}')
        print('聊天')
        print('约吗')
        print('约...')
    meet('女','18-25','学生',hight='170',weight='120')
    结果:
    左划一下
    右划一下
    寻找性别女,年龄18-25岁,工作学生,身高170,体重120
    聊天
    约吗
    约...

    1.3 Katachisan

    Parameter in the function definition portion becomes the parameter can be divided into location parameters, default parameters, the position of the dynamic parameters, the dynamic parameters of keywords

    Location parameters: the angular position parameters as argument

    Default parameters: some parameters in the rear position, i.e., do not pass the parameters follow the default parameters.

    # open('文件的改',encoding='utf-8')
    def meet(age,job,sex='女'):
        print('左划一下')
        print('右划一下')
        print(f'寻找性别{sex},年龄{age}岁,{job}')
        print('聊天')
        print('约吗')
        print('约....')
    # meet('18~25','幼师')
    # 更改默认参数
    # meet('18~25','幼师',sex='laddy_boy')
    meet('18~25','幼师','laddy_boy')

Guess you like

Origin www.cnblogs.com/yaoqi17/p/11040215.html