One, function definition
def function name (parameter list):
Function body
Find the sum of natural numbers within 100
# 求 100 之内自然数的和
# 方法一:内置 sum 函数
n = 100
print(sum(range(1, n+1)))
# 方法二:for循环
s = 0
for i in range(1, n+1):
s += i
print(s)
# 方法:自定义函数
def f(n):
'''
求 100 以内的自然数的和 # 这里是函数的说明文档,doc的位置
:param lis: n 是自然数 # 参数列表的说明
:return: 和 # 返回值的说明
'''
s = 0
for i in range(1, n+1):
s += i
return s
print(f(100))
print(f.__doc__)
The return [expression] statement is used to exit a function and optionally return an expression to the caller.
The return statement without parameter values or the default return returns None.
Return multiple values will be automatically packed into a tuple, receiving can use multiple variables (unpacking) can also use a variable (tuple)