python常用内置函数

Python所以内置函数如下:

下面列举一些常用的内置函数:

chr()和ord()

1 chr()将数字转换为对应的ascii码表字母
2 >>> r=chr(65)
3 >>> print(r)
4 A
1 ord()将字母转换为对应的ascii码表数字
2 >>> n=ord('a')
3 >>> print(n)
4 97

需要注意的是,中文汉字也可以。

1 >>> ord("") 
2 23384
3 >>> chr(23384)
4 ''
5 >>> 

join(): 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串

下面给出一个join函数使用的示例:#join无法将字符型和整型相加

 1 import random
 2 li=[]
 3 for i in range(6):
 4     r=random.randrange(0,6)
 5     if r==2 or r==4:
 6         num=random.randrange(0,10)
 7         li.append(str(num)) #使用str将num转换为字符型,因为在后面使用join时,join无法将字符型和整型相加
 8     else:
 9         tem=random.randrange(65,91)
10         c=chr(tem)
11         li.append(c)
12 result=''.join(li)
13 print(result)

compile()#将字符串编译成python代码

exec() #exec执行编译后的代码,没有返回值

eval() #eval只能处理python的表达式,有返回值

1 >>> s='print (123)'
2 >>> r=compile(s,"<string>","exec") #不写入<string>,则应当写文件#名称,第三处可有三种写法,分别是:single单行代码  eval 表达式 exev等#同于python代码
3 >>> exec(r) exec执行编译后的代码,没有返回值
4 123
5 >>> exec("print('hello world')")
6 hello world
1 >>> s='3+2'
2 >>> eval(s)
3 5

dir():查看对象的方法
help():查看对象的方法和使用#可查看源码

 1 >>> dir(str)
 2 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', .............略
 3 
 4 >>> help(str)
 5 Help on class str in module builtins:
 6 
 7 class str(object)
 8  |  str(object='') -> str
 9  |  str(bytes_or_buffer[, encoding[, errors]]) -> str
10  |  
11  |  Create a new string object from the given object. If encoding or
12  |  errors is specified, then the object must expose a data buffer
13  |  that will be decoded using the given encoding and error handler.
14  |  Otherwise, returns the result of object.__str__() (if defined)
15  |  or repr(object).
16  |  encoding defaults to sys.getdefaultencoding().
17  |  errors defaults to 'strict'.
18  |  
19  |  Methods defined here:
20  |  
21  |  __add__(self, value, /)
22  |      Return self+value.
23 ........省略

 

divmod()#得到两个数字的商和余数

1 >>> divmod(45,10)
2 (4, 5)

 

isinstance()  #判断对象是哪个类的实例

1 >>> s=[11,22,33]
2 >>> r=isinstance(s,list)
3 >>> print(r)
4 True

 filter(函数,可迭代对象) #对序列做过滤处理

#循环第二个参数,执行第一个参数函数,将第二个参数作为函数的参数,如果返回结果为True,则接受

 1 def f2(arg):
 2     if arg>22:
 3         return True
 4 li=[11,22,33,44,55]
 5 ret=filter(f2,li)
 6 print(list(ret))
 7 
 8 
 9 ##################################################################
10 def f1(arg):
11     result=[]
12     for item in arg:
13         if item > 22:
14             result.append(item)
15     return result
16 li=[11,22,33,44,55]
17 ret=f1(li)
18 print(ret)
19 
20 ###################################################
21 li=[11,22,33,44,55]
22 result=filter(lambda a:a>33,li) #lambda表达式默认返回函数体的值,和普通函数more返回值为None不相同
23 print(list(result))

map(函数,可迭代对象)  #将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回

 1 li=[11,22,33,44,55]
 2 def f1(arg):
 3     result=[]
 4     for i in arg:
 5         result.append(100+i)
 6     return result
 7 r=f1(li)
 8 print(list(r))
 9 
10 #############################################
11 li=[11,22,33,44,55]
12 def f2(a):
13     return a+100
14 result=map(f2,li)
15 print(list(result))
16 ######################################################
17 result=map(lambda  a:a+100,li)
18 print(list(result))

len()#计算对象的长度

注:python2使用字节计算,python3使用字符来计算,若果想按字节计算,可以先使用bytes转换成字节

1 >>> s='李杰'
2 >>> print(len(s))
3 2
4 >>> b=bytes(s,encoding='utf-8')
5 >>> print(len(b))
6 6
7 #不明白的可以使用for循环李杰,在2.7和3.5看结果有什么不一样

max()返回给定参数的最大值,参数可以为序列
min()返回给定参数的最小值,参数可以为序列

1 >>> max(151,48,45)
2 151
3 >>> min(151,48,45)
4 45

pow()#求幂

>>> pow(2,3)
8
>>> pow(2,10)
1024

round()#四舍五入

1 >>> round(1.8)
2 2
3 >>> round(2.4)
4 2

zip()#接受任意多个序列作为参数,返回一个tuple列表

 1 python3.5 返回一个迭代器对象
 2 >>> a=(1,2,3)
 3 >>> b=('a','b','c')
 4 >>> c= zip(a,b)
 5 >>> print(c)
 6 <zip object at 0x02A5CCD8>
 7 
 8 
 9 python2.7 返回一个元组
10 >>> a=(1,2,3)
11 >>> b=('a','b','c')
12 >>> c= zip(a,b)
13 >>> print(c)
14 [(1, 'a'), (2, 'b'), (3, 'c')]

any()#给定的对象中只要有一个对象bool值为true时返回true。

all()#给定的对象中只有全部对象bool值为true时返回true。

1 >>> any([11,'a',[]])
2 True
3 >>> all([11,'a',[]])
4 False

 reduce()#将一个数据集合(列表,元组等)中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果

 1     def myadd(x,y):  
 2         return x+y  
 3     sum=reduce(myadd,(1,2,3,4,5,6,7))  
 4     print sum  
 5 
 6 #结果就是输出1+2+3+4+5+6+7的结果即28
 7 也可以用lambda的方法,更为简单:
 8     sum=reduce(lambda x,y:x+y,(1,2,3,4,5,6,7))  
 9     print sum 
 在python 3.0.0.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce.

 

posted @ 2016-05-30 15:17  百衲本  阅读(457)  评论(0编辑  收藏  举报
cnblogs_post_body { color: black; font: 0.875em/1.5em "微软雅黑" , "PTSans" , "Arial" ,sans-serif; font-size: 15px; } cnblogs_post_body h1 { text-align:center; background: #333366; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 23px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h2 { text-align:center; background: #006699; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 20px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h3 { background: #2B6695; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 18px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } 回到顶部 博客侧边栏 回到顶部 页首代码 回到顶部 页脚代码