1 function1.1 calling Functions
To invoke a function, you need to know the name and parameters of the function .
ABS Absolute Value function
>>> ABS (-10)
10
>>> ABS (-213)
213
Max Maximum value function
>>> Max ( -1,2,5)
5
Data type conversions
>>> Int (12.3)
12
>>> int (' 12.3 ')-- when converting an integer string with decimals, an error will be
Traceback (most recent):
File "<stdin>", line 1, in <module>
Valueerror:invalid literal for int () Withbase 10: ' 12.3 '
>>>Int (' + ')-- converts an integer string without a decimal to an error
12
>>>Int (float (' 12.3 ')) --Transforms with the float function
12
>>> float (' 12.3 ')
12.3
>>> Str (12.3)-- string function
' 12.3 '
>>> STR (100)
' 100 '
>>> BOOL (1)
True
>>> BOOL (0)
False
>>> bool (')
False
1.2 Defining Functions
in the in Python, define a function to use the DEF statement, write down the function name, parentheses, the arguments in parentheses, and the colon : andThen, in the indent block, write the function body, and the return value of the function is returned with a return statement.
def my_abs (x):
If x >= 0:
return x
Else
Return–x
The statement inside the function body executes when it executes to the return , the function is executed and the result is returned .
if not return Statement , the result is returned after the function is executed, only the result is None .
Return None can be shortened to return.
1.2.1 in an interactive environment
>>> def my_abs (x):
... if x>= 0:
... return x
... if x< 0:
... return-x
... – requires two times enter key
>>> My_abs (-1)
1
>>> My_abs (-8.1)
8.1
in the when defining functions in the Python interactive environment, be aware that python will appear ... the prompt. After the function definition is finished, press two to return to the >>> prompt .
1.2.2 non-interactive environment
[Email protected] python]# VI my_abs.py
#!/usr/bin/python
#-*-Coding:utf-8-*-
def my_abs (x):
If x >= 0:
return x
Else
Return–x
>>> from My_abs Import my_abs--the first my_abs is a py file, the second my_abs is a function
>>> My_abs (-1)
1
1.2.3 null function
Define an empty function
>>> def pop ():
... pass--pass means nothing and can also be used in the If judgment, similar to null in plsql
...
>>> pop ()
>>>
1.2.4 parameter Check
upgrade The My_abs function to check the input parameters
>>> def MY_ABS1 (x):
... if not isinstance (x, (Int,float)):--isinstance for data checking
... raise TypeError (' bad Oprand type ')
... if x >=0:
... print (x)
... if x <0:
... print (-X)
...
>>>
>>> my_abs1 (' Y ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in MY_ABS1
Typeerror:bad Oprand Type
1.2.5 function returns multiple values
[email protected] python]# cat move.py
#!/usr/bin/python
#-*-Coding:utf-8-*-
Import Math
def move (x,y,step,angle=0):
NX = x + step * Math.Cos (angle)
NY = y + step * math.sin (angle)
Return NX, NY
>>> Import Math
>>> T=move (+, 60,MATH.PI/6)
>>> Print (t)
(151.96152422706632, 130.0)-- Essentially, a tuple is returned
>>> x, y = Move (+, 60,MATH.PI/6)-- Multiple variables are assigned by location
>>> print (x, y)
151.96152422706632 130.0
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1795760
Python-Defined Functions