Definition of Python function
Defining a function, that is, creating a function, can be understood as creating a tool with certain uses. The function definition needs to be realized with the def keyword. The specific syntax format is as follows:
def function name(Parameter list)://A code block composed of zero to many executable statements[return[return value]]
Among them, the part enclosed in [] is optional, which can be used or omitted.
In this format, the meaning of each part of the parameters is as follows:
Function name: From a grammatical point of view, the function name can be a valid identifier; from the point of view of program readability, the function name should be concatenated with one or more meaningful words, each word The letters of are all lowercase, and words are separated by underscores.
Formal parameter list: used to define the parameters that the function can receive. The parameter list consists of multiple parameter names, separated by commas (,). Once the formal parameter list is specified when the function is defined, the corresponding parameter values must be passed in when the function is called, that is, who calls the function is responsible for assigning values to the formal parameters.
Note that when creating a function, even if the function does not require parameters, a pair of empty "()" must be reserved, otherwise the Python interpreter will prompt an "invaild syntax" error. In addition, if you want to define an empty function without any function, you can use the pass statement as a placeholder.
The following program defines two functions:
def my_max(x, y):
# Define a variable z, which is equal to the larger value of x and y
z = x if x y else y
# Returns the value of variable z
return z
# Define a function, declare a formal parameter
def say_hi(name):print("===Is executing say_hi()function===")return name +",Hello!"
Example extension:
Functions to manipulate the database. The parameters are not passed in order, and port = '3306' is used, which is very suitable for functions with multiple parameters.
def op_mysql(host,port,username,password,db,sql):print('Connect to the database:%s,host:%s, the port is:%s'%(db,host,port))print("%s where username='%s and pwd = '%s'"%(sql,username,password))op_mysql(sql='select * from user',
host='192.158.11.1',
username='sdfdsfs',
password='sdf123',
db='test_data',
port='3306')
Example: Write a function to realize the function of reading and writing files.
# The function of operating the file, if the content parameter is passed, the corresponding content will be written into the file. If no content is passed, the original content of the file is read.
def op_file(file_name,content=None):
f =open(file_name,'a+', encoding='utf-8')
f.seek(0)if content:#Not empty means write
f.write(content)
f.flush()else:
res = f.read()return res
f.close()
# Do not pass content, read files
stu_info =op_file('username')print(stu_info)
# Pass content, write content to file
infos ='xiaohei,123456,beijing,188888888888'op_file('new_info',infos)
So far, this article on the method of defining a function in Python has introduced it. For more information about how to define a function in Python, please search for ZaLou.Cn's previous articles or continue to browse related articles below. I hope you will support ZaLou more in the future. Cn!
Recommended Posts