python内置了很多函数,可以直接调用
http://docs.python.org/3/library/functions.html#abs
help(abs)
Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
- 要调用函数需要知道,函数的名称和参数。求绝对值得函数abs,只有一个参数;
abs(100)
100
abs(-100)
100
abs(1,2)
File "<ipython-input-4-b938361f070b>", line 1
abs(1,2)
^
SyntaxError: invalid character in identifier
abs('x')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-9549c27a5919> in <module>()
----> 1 abs('x')
TypeError: bad operand type for abs(): 'str'
- max函数可以有多个参数;
max(12,23,45)
45
数据类型转换
int('123')
123
int (12.34)
12
float(12.34)
12.34
str(1.23)
'1.23'
bool布尔函数,函数用于给定参数转换为布尔类型,没有参数返回false
bool(100)
True
bool('')
False
函数名其实是一个指向函数对象的引用,可以将函数赋值给变量,相当于取了别名;
a = abs
a(11)
11
a(-12)
12
- 定义函数
def my_abs(x):
if x >= 0:
return x
else:
return -x
print (my_abs(-99))
99
-
命令行执行定义的函数
函数.PNG 命令行调用定义好的函数文件