Home >Backend Development >Python Tutorial >What are the examples of python custom functions?
Recommended tutorial: "python video tutorial"
What are the examples of python custom functions?
Examples of python custom functions include:
1. What is a function?
A function is an organized, reusable code segment used to implement a single or related function. Functions can improve application modularity and code reuse.
2. Function definition method:
def test(x): '函数定义方法' x+=1 return x
Explanation:
def
: Define function keywords
test
: Function name
()
: Definable formal parameters
''
: Document description
x =1
: Code block or program processing logic
return
:End and return value
Why should a function have a return value? ?
Receive the execution result of the function through the return value, and subsequent logic needs to use this result to perform its corresponding operation.
(1), Example: Write log information to a file
import time # 定义函数 def test1(): '函数练习:添加日志记录' log_time = time.strftime('%Y-%m-%d %X') with open('file_a','a') as f: f.write(log_time+':log msg\n') # 调用函数 test1()
(2), Function return value description:
return Number of return values = 0: Return one None value (none)
Return value = 1: Object (Object)
rebate Value & GT; :
#函数返回类型 def test_None(): print('返回一个空值') x=test_None() print(x) def test_object(): print('返回一个对象') return 0 y=test_object() print(y) def test_tuples(): print('返回一个元组') return 1,'hello world',['qwe','asd'],{'001':'simple'} z=test_tuples() print(z)
(3), formal parameters: the defined parameters are called formal parameters (x, y)
Actual parameters: the actual parameters passed in are called actual parameters (1,2)
Without specifying parameters: one-to-one correspondence between actual and formal parameter positions
Example:
Note: When positional parameters are shared and keyword parameters are shared, keyword parameters cannot be written. Before positional parameters
def test_sum(x,y): '两数之和' z = x + y return z t_sum=test_sum(1,2) #实参与形参位置一一对应 print(t_sum) t_sum2=test_sum(x=1,y=2) #与形参位置无关 print(t_sum2) t_sum3=test_sum(1,y=2) # 错误方式:test_sum(x=1,2) 位置传参与关键字传参共用时,关键参数不能写在位置参数之前 print(t_sum3)
(4), default value parameter:
# 默认值参数 def test_default(x,y=2): '默认值参数' z = x + y return z print(test_default(2)) #print(tesst_default(2,2))
(5), parameter group (indefinite length parameter):
# 参数组,接受位置参数,将多个实参存入一个元组中 # 定义格式:*变量名(一般规范为 *args) def test_group(*args): '参数组' print(args) test_group(1,2,3,4,5,6) def test_group2(x,*args): '参数组与位置参数混用' print(x) print(args) test_group2(1,2,3,4,5,6) # 接受关键字参数组,转换成字典 def test_group3(**kwargs): '参数组键值对形式' print(kwargs['name']) test_group3(name='simple',age=25,sex='m') def test_group4(name,**kwargs): '参数、参数组键值对混用' print(name) print(kwargs) test_group4('simple',age=25,sex='m') #注:参数组必须放在位置参数、默认参数之后 def test_group5(name,age=25,**kwargs): '参数、默认参数、参数组键值对混用' print(name) print(age) print(kwargs) test_group5('simple',age=3,sex='m',game='lol') def test_group6(name,age=25,*args,**kwargs): '参数、默认参数、参数组、参数组键值对混用' print(name) print(age) print(*args) print(kwargs) test_group5('simple',age=3,sex='m',game='lol')
Recommended related articles: "
python tutorialThe above is the detailed content of What are the examples of python custom functions?. For more information, please follow other related articles on the PHP Chinese website!