内置函数

     

例:如果返回数字的绝对值 ,写函数是非常不方便的
[root@zabbix tools]# python fa.py 
10
[root@zabbix tools]# cat fa.py 
#!/usr/bin/python
def a(x):
    if x < 0 :
        return -x 
    return x 
n = a(-10)
print n

 #帮助查看#

>>> help(len)

用法: help(函数名)

 abs()  返回绝对值

>>> abs(-10)
10

 max()返回最大值 , min()返回最小值

>>> l=[1,4,7,12,555,66,]
>>> min(l)
1
>>> max(l)
555

len() 获取字符串长度

>>> s = 'konglingchao'
>>> len(s)
12

 divmod() 求商,求余 用法:divmod(除数,被除数)

>>> divmod(10,2)
(5, 0)

pow() 次方运算  用法: pow(数1,数2,/(数3))  数3可有可无

>>> pow(3,3)
27
>>> pow(3,3,2)
1

round()返回浮点数

>>> round(1112)
1112.0

callable()测试函数是否被定义

>>> callable(min)
True                              #函数定义返回True
>>> callable(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'f' is not defined    #函数未被定义
>>> f=100                 #f是一个变量
>>> callable(f)
False                           #返回True

isinstance()

>>> print l
[1, 4, 7, 12, 555, 66]
>>> print f
<function f at 0x1a1e320>
>>> type(f)
<type 'function'>
>>> type(l)
<type 'list'> 
>>> if type(l) ==type([]):
...     print 'ok'
... 
ok      #判断是否为列表
使用isinstance 判断某一个对象类型
>>> isinstance(l,int)
False
>>> isinstance(l,str)
False
>>> isinstance(l,list)
True

cmp ()字符串比较

>>> cmp("7","7")
0
>>> cmp("7","m")
-1
>>> cmp("7","6")
1

range()快速生成序列

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

xrange()   可以 理解为对象,效率比range高

>>> x = xrange(10)
>>> print xxrange(10)

数据转换类型的函数:

int()  整数

long() 长×××

float() 浮点型

complex() 复数型

str()

list()

tuple()


>>> L=[1,2,3,4]
>>> tuple(l)
(1, 4, 7, 12, 555, 66)

注意: 数据转换要有选择性的转换

>>> s='hello'
>>> int(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'
>>> s='123'
>>> type(s)
<type 'str'>
>>> int(s)
123

string 函数 :功能只限于对字符串的操作

#查看帮助要 加上 函数的类别:>>> help(str.replace) 

  str.capitalize()  :首字母大写

>>> s='hello'
>>> str.capitalize(s)
'Hello'
>>> s
'hello word'
>>> s.capitalize()  #s是字符串里的对象,s调用capitalize函数
'Hello word'

  str.replace() :替换,(可以指定替换次数)

>>> s.replace('hello','good')
'good word'
>>> s
'hello word'
#这里的good word 只是一个返回值,并没有将数据直接改变
>>> ss='12121212121212'
>>> ss.replace('1','xxxxx')
'xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2'
>>> ss.replace('1','xxxxx',1)   #指定替换的次数
'xxxxx2121212121212'

  str.split() :切割  str.split(“参数1:切割服”,“参数2=切割次数”)

>>> ip="192.168.1.2"
>>> ip.split('.')
['192', '168', '1', '2']
>>> ip.split('.',2)
['192', '168', '1.2']

#######################

通过导入模块调用方法

>>> import string 
>>> help(string.replace)
>>> s
'hello word'
>>> string.replace(s,'hello','good')
'good word'

#####################################

序列处理函数

len()

max()

min()

#使用序列函数求数字的长度是无法使用的!!!!!

>>> len(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>>

filter (参数1:函数,参数2:序列)

Help on built-in function filter in module __builtin__:
filter(...)
    filter(function or None, sequence) -> list, tuple, or string
              #函数    或者 #空   序列
    Return those items of sequence for which function(item) is true.  If
    function is None, return the items that are true.  If sequence is a tuple
    or string, return the same type, else return a list.

范例:

>>> def f(x):
...     if x>5:
...         return True
... 
>>> f(10)
True
>>> f(3)
>>> l=range(10)
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> filter(f,l)
[6, 7, 8, 9]
# f 函数作用于列表l

zip、map()列表遍历

>>> name=['milo','zou','tom']
>>> age=[20,30,40]
>>> tel=['133','144','155']
>>> zip(name,age,tel)
[('milo', 20, '133'), ('zou', 30, '144'), ('tom', 40, '155')]
#并行遍历
>>> map(None,name,age,tel)
[('milo', 20, '133'), ('zou', 30, '144'), ('tom', 40, '155')]
>>> test=['a','b']
#如果其中一个列表的值少于其它列表,它会取最短的列表进行遍历;
>>> zip(name,age,tel,test)
[('milo', 20, '133', 'a'), ('zou', 30, '144', 'b')]
如果换为map遍历,它会将某一列表缺少的值用None填充。
>>> map(None,name,age,tel,test)
[('milo', 20, '133', 'a'), ('zou', 30, '144', 'b'), ('tom', 40, '155', None)]
#此处的None也可以变为函数,把后面的值进行运算。
>>> a=[1,4,6,]
>>> b=[7,8,9]
>>> map(None,a,b)
[(1, 7), (4, 8), (6, 9)]
>>> def mf(x,y):
...     print x*y
... 
>>> map(mf,a,b)
7
32
54
[None, None, None]

reduce()递归 运算

>>> def f(x,y):
...     return x+y
... 
>>> l=(range(1,101))
>>> reduce(f,l)
5050
或者使用匿名函数
>>> reduce(lambda x,y:x+y,l)
5050