First, the function
1, the function definition
def function name (parameter list):
<body>
2, returns the return value
The return value can have a plurality of
## a plurality of return value DEF sumDiff (X, Y): return X + Y, XY N1, N2 = the eval (INPUT ( 'Enter two numbers:')) S, D = sumDiff (N1, N2) Print ( ' and {} is the difference for the {} '. format (s, d))
Calculating perimeter of the triangle ## Import Math DEF Square (X): return X * X # calculating the distance between two points DEF Distance (X1, X2, Y1, Y2): dist = Math.sqrt (Square (X1-X2) Square + (Y1-Y2)) return dist # determines whether or not three points triangles DEF isTriangle (X1, Y1, X2, Y2, X3, Y3): In Flag = ((X1-X2) * (Y2-Y3) - ( X2-X3) * (Y1-Y2)) = 0! return In Flag DEF main (): Print ( 'enter three coordinate point (X, Y):') X1, Y1 = the eval (iNPUT ( '1 coordinate point ( X1, Y1) = ')) X2, Y2 = the eval (INPUT (' coordinate point 2 (X2, Y2) = ')) X3, Y3 = the eval (INPUT (' coordinate point 3 (x3, y3) = ' )) # determines whether or not the three points form a triangle iF (isTriangle (X1, Y1, X2, Y2, X3, Y3)): ## calculated perimeter of the triangle Perim Distance = (X1, Y1, X2, Y2) + Distance (X2, Y2, X3, Y3) + distance (x3, y3, x1, y1) Print ( 'the perimeter of the triangle as: {:. 2f}'. the format (Perim)) the else: Print ( 'not three points form a triangle') main ()
3, parameter
If the variable is an object variable [list], returns to the calling program, the state of the object will appear after being modified
def addInterest(balances,rate): for i in range(len(balances)): balances[i] = balances[i] * (1+rate) def test(): amounts = [1000,500,1200,789] rate = 0.05 addInterest(amounts,rate) print(amounts) test() ##[1050.0, 525.0, 1260.0, 828.45]