Python functions: in fact, they are very similar to C ++, but because they are weak languages (it feels like they are ......), remove all types of declarations, and pass the value instead of reference (at least 2.7 is ), A small number of cells are defined by def (like macro definition ......), the following is an example of Python function: it is actually very similar to C ++, but it is a weak type of language (it feels like it is ......), remove all types of declarations, and pass the value instead of reference (at least 2.7 is ), A small number of cells are defined by def (like macro definition ......), the following is an example.
Def sum (a, B): # sum is the function name. pay attention to the colon ......
C = a + B # This part is the statement block of the function.
Return c # The function ends here. like C ++, PRint sum (1, 2) # Call the function based on the function name and output the value of 1 + 2.
Lambda functions are also provided here, which are similar to the features of C ++ 11. An example of the above addition function is provided.
Sum = lambda a, B: a + B # This is the definition of the lambda function. it starts with lambda and then is the input parameter. no matter how many parameters are separated by ",", and then ": "The following is the expression print sum (1, 2)
Python module: this part of feeling has something to do with java. However, the module gives me the feeling that the header file in C ++ and the file written together with the implementation are all. the following is an example.
First, the code of the Add function module is provided. if there is a code in fun. py of the current directory
#! /Usr/bin/pythondef sum (a, B ):
C = a + B return c
The code of the main function is provided. if the name test. py exists in the current directory, use import to import the module fun (isn't this Java ......),
#! /Usr/bin/pythonimport funprint fun. sum (1, 2)
Sometimes it is very troublesome. that fun is the same as the C ++ namespace, so we can actually use sum through from... import the import statement to the current space and give the code
#! /Usr/bin/pythonfrom fun import sumprint sum (1, 2)
Sometimes there may be a path strength problem. for now, make sure that the imported module is in the same directory as the main program.
The above is the content of the function and module in Python-Article 4. For more information, see PHP Chinese website (www.php1.cn )!