Python function definition related + "class" as function parameter

Definition of simple functions

def 函数名(形参1, 形参2...):
    语句1
    语句2
    ...
    return xxx

1. There can only be one function return value
. 2. If the function statement writes pass, it is an empty function, which is used as a placeholder. For example, if you haven't figured out how to write the function code, you can put a pass first, so that the code can run stand up.
3. The variables in the function statement use formal parameters
4. The return statement only write return is equivalent to return 0

Define a function with multiple return values

def 函数名(形参1, 形参2...):
	语句1
    语句2
    ...
    return xxx, yyy

1. Actually returns a "tuple", the element corresponds to multiple values ​​of the return statement
2. Tuple access: tuple name [subscript]

List or tuple as function parameter

def 函数名(*列表名):          #注意:函数定义时,列表或元组类型的形参要在名前加一个 *
    语句1
    语句2
    ...
    for n in 列表名:         #便遍历列表元素 
        语句1
        语句2
    	...
    return x
print(函数名(*列表名))      #注意:对应的,函数调用时实参前面也要加一个 * 

Data Type 1. A list of all elements must have a digital type

Supplement: How to directly input user input to generate a list

Since the list is used as a function argument, the data type conversion of the list element is also required
.

contents = input()               #读取用户输入(默认为字符类型)
list1 = contents.split(',')      #使用.split方法分割输入的内容,生成字符型列表
for i in range(len(list1)):      #将原列表的所有元素全部转化为整数类型
    list1[i] = int(list1[i]) 

The list element type conversion can also be replaced as follows:

list1 = list(map(int,list1))     #将原列表的所有元素全部转化为整数类型

map(function,iterable) function: run all elements in iterable as function parameters once, and the return type is iterable (so you need to add a step list() function to generate the list), if the function writes the data type, it means that all elements Type forced transfer

Guess you like

Origin blog.csdn.net/kyc592/article/details/110958414