Tissue function is good, reusable, code segments used to implement a single, or the associated function.
Function module of the application can be improved, and code reuse. You already know that Python provides many built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions.
The definition of a function
You may want to define a function by their own functions, the following simple rules:
- In function block def beginning keyword, followed by the name and function identifier parentheses () .
- Any incoming parameters and arguments in parentheses must be placed in the middle, between parentheses may be used to define the parameters.
- The first statement function can be selectively used documentation string - for storing function instructions.
- Start function contents colon, and indentation.
- return [Expression] End function, selectively returns a value to the caller. Return without an expression equivalent to return None.
grammar
Python custom function uses def keyword, the following general format:
def function name (parameter list): function body
By default, the parameter name and the parameter value is defined in order to match the function declaration together.
characteristic
1, the function must be called before execution, the function calls need to write the function name and ()
2, the return value of the function, a function of the time required to return results
3, which functions as long as the encounter return, the function ends immediately after the code is not executed
4, parameters: a function to perform its intended work item of information, such as def user (username), username parameter is
5, arguments: the function call to the information transfer function, we call the function, the information you want to use the function in parentheses, such as user ( 'linda'), 'linda' as argument;
For example
1, it is judged whether the input digital decimals
S =input("请输入数字:") def is_float(num): num = str(num) if num.count('.') == 1: left,right = num.split('.') if left.isdigit() and right.isdigit(): print("是正小数") elif left.count('-') == 1 and left.startswith('-') andleft [. 1 :] isdigit ():. Print ( " negative decimal " ) the else : Print ( " illegal decimal " ) the else : Print ( " not a decimal " ) of is_float (S) # call the function of is_float (for 1.5) # positive decimals of is_float (-23.2) # negative decimals of is_float ( ' -R.5 ' ) # illegally decimal of is_float ( ' -1.3.2 ' ) # instead of decimal of is_float (35) # instead of decimal
DEF of is_float (NUM): NUM = STR (NUM) IF num.startswith ( ' - ' ) and num.count ( ' - ' ). 1 ==: # determines whether or not beginning with a minus NUM = num.replace ( ' - ' , '' ) # if yes, the negative sign is replaced with an empty IF num.count ( ' . ' ) ==. 1 and NUM [:. 1]! = ' . ' and NUM [-1:] =! ' . ' : # determine whether to include a decimal point, and decimal point is not the beginning of the end num.replace = NUM ( ' . ' , '' ) # if yes, the decimal point is replaced with an empty IF num.isdigit (): # determines whether an integer Print ( ' Xiaoshu ' ) return True return False Print (of is_float (. 1. )) # print result is True Print (of is_float ( ' 1. ' )) # print result is False Print (STR (. 1.)) # print result was 1.0