First, the basic steps of Python custom function
1. What is a function
print()
For example, functions that appear in basically every chapter .print('两点水')
, if we want to print 两点水
it , we 两点水
feed this data to the print
function, and it prints the result directly to the console.2. How to customize the function
How to customize the function?
To know how to define a function, you need to know what the components of a function look like.
def 函数名(参数1,参数2....参数n):
函数体
return 语句
-
A block of function code begins with the def keyword, followed by the function identifier name and parentheses ()
-
Any incoming parameters and arguments must be enclosed in parentheses. between parentheses can be used to define parameters
-
The first line of the function can optionally use a docstring (for function description)
-
Function content starts with a colon and is indented
-
return [expression] Ends a function, optionally returning a value to the caller. return without an expression is equivalent to returning None.
def functionname( parameters ):
"函数_文档字符串"
function_suite
return [expression]
-
def defines a function, given a function name sum
-
declare two parameters num1 and num2
-
The first line of the function statement describes the function: the sum of two numbers
-
The final return statement ends the function and returns the sum of the two numbers
def sum(num1,num2):
"两数之和"
return num1+num2
# 调用函数
print(sum(5,6))
Output result:
11
Featured Article Recommendations
Follow me and catch the latest and most trendy black technology together