Python defines a function method

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

Python defines a function method
Python enumerate() function
Is there a helper function in python
Python function buffer
Python magic method topic
Python object-oriented magic method
Python custom function basics
Python3.7 debugging example method
Join function in Python
Python built-in function -compile()
Python function basic learning
Python data analysis-apply function
Python function-dictionary get() method
Python3 built-in function table.md
Python error handling method
Python Print print timer function
Python high-order function usage summary!
Python TCP packet injection method
Python realizes online translation function
Python implements a guessing game
Python drawing ring graph method
Python tracking except information method
Python tornado upload file function
Python magic function eval () learning
How Python implements FTP function
Python implements gradient descent method
Python right alignment example method
Python method of parameter passing
Python implements image stitching function
Python arithmetic sequence calculation method
Python high-order function usage summary!
Python telnet login function implementation code
Python| function using recursion to solve
Method steps to increase python font
Summary of logarithm method in Python
Why call python a glue language
Python and js interactive call method
How Python implements the mail function
Why doesn't Python support function overloading?
Is there function overloading in python
Learning Python third day one-line function
Python function definition and parameter explanation
Python is a cross-platform language code
Python implements a universal web framework
Python ATM function implementation code example
A summary of 200 Python standard libraries!
Python implements AI face change function
Python implements the steepest descent method
Can python become a mainstream language?
Python realizes image recognition car function
Python implements ftp file transfer function
Python realizes udp transmission picture function
How Python implements the timer function
End the method of running python
Python draws a visual line chart