Python's Calling function
Python has built in a lot of useful functions that we can call directly.
To invoke a function, you need to know the name and parameters of the function , such as an absolute function abs, which receives a parameter.
Documents can be viewed directly from the official website of Python: http://docs.python.org/2/library/functions.html#abs
You can also view the Help information for the ABS function on the interactive command line by helping (ABS).
Call the abs function:
>>> ABS (100>>>) ABS ( -20) 20>>> abs (12.34) 12.34
When calling a function, if the number of arguments passed in is incorrect, the TypeError error is reported, and Python will explicitly tell you that ABS () has only 1 parameters, but gives two:
>>> ABS (1, 2) Traceback (most recent): File "<stdin>", line 1, in <module>typeerror:abs () takes exactly one argument (2 given)
If the number of arguments passed in is correct, but the parameter type cannot be accepted by the function, the TypeError error is reported, and an error messageis given: STR is the wrong parameter type:
>>> ABS (' a ') Traceback (most recent call last): File "<stdin>", line 1, in <module>typeerror:bad Operand type for abs (): ' Str '
The comparison function cmp (x, y) requires two parameters, if x<y, returns 1, if x==y, returns 0, if x>y, returns 1:
>>> CMP (1, 2) -1>>> CMP (2, 1) 1>>> CMP (3, 3) 0
Python's built-in common functions also include data type conversion functions, such as the int () function, which converts other data types to integers:
>>> Int (' 123 ') 123>>> Int (12.34) 12
The STR () function converts other types to str:
>>> STR (123) ' 123 ' >>> str (1.23) ' 1.23 '
Task
The sum () function takes a list as a parameter and returns the sum of all the elements of the list. Please calculate 1*1 + 2*2 + 3*3 + ... + 100*100.
Reference code:
L = Range (1,101)
Print sum (x**2 for x in L)
Python's writing function
In Python, define a function to use the def statement, write down the function name, parentheses, the arguments in parentheses, and the colon: And then, in the indent block, write the function body, return value of the function with return statement is returned.
Let's take the example of a custom my_abs function that asks for an absolute value:
def my_abs (x): if x >= 0: return x else: return-x
note that when the statement inside the function body executes, once it executes to return, the function finishes and returns the result. Therefore, it is possible to implement very complex logic within a function through conditional judgments and loops.
If there is no return statement, the result is returned after the function is executed, except that the result is none.
Return none can be shortened to return.
Task
Define a square_of_sum function that takes a list and returns the sum of the squares of each element in the list.
Reference code:
def square_of_sum (L):
Sum=0
For x in L:
Sum=sum+x*x
return sum
Print Square_of_sum ([1, 2, 3, 4, 5])
Print Square_of_sum ([-5, 0, 5, 15, 25])
Can a function return multiple values? The answer is yes.
For example, in the game often need to move from one point to another point, give the coordinates, displacements and angles, you can calculate the new coordinates:
# The Math package provides the sin () and cos () functions, which we first refer to with import:
Import mathdef Move (x, y, step, Angle): NX = x + step * math.cos (angle) NY = y-step * Math.sin (angle) return NX, NY
So we can get the return value at the same time:
>>> x, y = Move (MATH.PI/6) >>> print x, y151.961524227 70.0
But this is just an illusion, and the Python function returns a single value:
>>> r = Move (MATH.PI/6) >>> print R (151.96152422706632, 70.0)
Prints the returned result with print, the original return value is a tuple!
However, in syntax, the return of a tuple can omit parentheses, and multiple variables can receive a tuple at the same time, by the location of the corresponding value, so thepython function returns a multi-value is actually returned a tuple, but it is more convenient to write.
The definition of a unary two-second equation is:ax2 + bx + c = 0
Write a function that returns the two solutions of the two-second equation.
Reference code:
Import Math
Def quadratic_equation (A, B, c):
X=MATH.SQRT (B*B-4*A*C)
Return (-B+X)/(2*a), (-b-x)/(2*a)
Print Quadratic_equation (2, 3, 0)
Print quadratic_equation (1,-6, 5)
Python's Calling function