python成长之路10——内置函数

1. callable(): 查看对象是否能被调用,可用于检查对象是否是函数。

 1 #callable()
 2 def f1():
 3     pass
 4 f2="a"
 5 print(callable(f1))
 6 print(callable(f2))
 7 
 8 执行结果:
 9 True
10 False

2. chr():将ascii码转换成对应的字符

  ord():相反

1 print(chr(65))
2 print(ord("A"))
3  
4 执行结果:
5 A
6 65

小例子:

 1 import random
 2 def check_code():
 3     li=[]
 4     for i in range(6):
 5         r=random.randrange(0,5)
 6         if r == 2:
 7             temp=random.randrange(0,10)
 8             li.append(str(temp))
 9             #join方法要求列表里的每个元素必须是字符串
10         elif r == 4:
11             temp=random.randrange(97,123)
12             k=chr(temp)
13             li.append(k)
14         else:
15             temp=random.randrange(65,91)
16             k=chr(temp)
17             li.append(k)
18     return "".join(li)
19 ret=check_code()
20 print(ret)
21 
22 执行结果:
23 FV16NY
生成一个6位的随机验证码

3. compile():将字符串编译成python代码

  exec():执行

  eval():执行,可用于将"{k1:v1,k2:v2}"变成{k1:v1,k2:v2}

  区别:eval主要是用来执行表达式的,有返回值,而exec要比eval的功能更强大,可以直接执行pyhon代码(字节码)或者字符串(如果接收的是字符串的话要先执行compile),但是它没有返回值。

python解释器执行文件要经过以下几个步骤:

  • 读取文件内容到内存     open—str到内存                   open完成
  • python解释器把字符串  str—编译成—字节码             compile完成
  • 执行代码                        字节码—机器码                        exec完成

compile有三种编译模式:

  • single:编译成单行的python程序
  • eval:编译成表达式
  • exec:编译成和python代码一模一样的东西
 1 with open("s1.py","r",encoding="utf8") as f:
 2     print("----打印代码-----")
 3     print(f.read())
 4     f.seek(0)
 5     print("-----执行代码-----")
 6     r=compile(f.read(),"<string>","exec")
 7     exec(r)
 8 
 9 执行结果:
10 ----打印代码-----
11 #/usr/bin/env python
12 #-*- coding:utf-8 -*-
13 #Authot:Zhang Yan
14 print("我是s1.py")
15 
16 -----执行代码-----
17 我是s1.py
18 
19 s1="print('我是s1.py')"
20 r=compile(s1,"<string>","exec")
21 print(r)
22 print(type(r))
23 exec(r)
24 
25 执行结果:
26 <code object <module> at 0x102137c90, file "<string>", line 1>
27 <class 'code'>
28 我是s1.py
exec编译模式
 1 s1="print('我是单行的')"
 2 r1=compile(s1,"<string>","single")
 3 exec(r1)
 4 
 5 执行结果:
 6 我是单行的
 7 
 8 s2='''
 9 print("我是第一行")
10 print("我是第二行")
11 '''
12 r2=compile(s2,"<string>","single")
13 exec(r2)
14 
15 执行结果:
16 Traceback (most recent call last):
17   File "/Users/admin/stu179471/day3/zy.py", line 13, in <module>
18     r2=compile(s2,"<string>","single")
19   File "<string>", line 2
20     print("我是第一行")
21                   ^
22 SyntaxError: multiple statements found while compiling a single statement
single编译模式

4. dir():快速查看一个对象可以提供什么功能,不显示功能详细

 help():显示功能详细

5. divmod():得到商和余数,返回值是元祖,可以这样用:n1,n2=divmod(97,10)则n1是商,n2是余数

6. isinstance():判断对象是否是某个类的实例,如果是的话,返回True,instance(对象,类)

7. filter(函数,可迭代的对象):循环可迭代对象中的元素,作为函数的参数,来执行函数。

 1 def f1(a):
 2     if a>0:
 3         a=a+1
 4         return a
 5 li=[-1,11,22,33,44]
 6 ret=filter(f1,li)
 7 print(ret)
 8 print(type(ret))
 9 #filter(函数,可迭代对象) 可迭代对象的每个元素作为f1的参数a执行函数
10 #filter(f1,li)的返回值ret为filter对象,也是一个可迭代的对象
11 #如果函数f1返回值为True,表示当前元素合法,则当前元素就会变成filter对象的元素
12 #上面的例子就是:如果li的元素>0,则a=a+1,肯定大于0,return a就相当于return True,则此元素合法
13 #所以用filter(f1,li)时,f1判断元素合法可以直接写return True
14 
15 #还有一点要注意:filter的返回值是类似于队列的一种东西,就是说取了这个值之后再取就没啦
16 print(list(ret))
17 print(list(ret))
18 
19 执行结果:
20 <filter object at 0x102182048>
21 <class 'filter'>
22 [11, 22, 33, 44]
23 []
filter
1 #lambda和filter结合
2 li=[11,22,33,44]
3 ret=filter(lambda a: a>22,li)
4 print(list(ret))
5 
6 执行结果:
7 True
8 [33, 44]
filter和lambda结合

8.map(函数,可迭代对象):循环可迭代对象中的元素,作为函数的参数,来执行函数。

 1 li=[11,22,33,44,55]
 2 def f1(a):
 3     if a>22:
 4         return a+100
 5     else:
 6         return a
 7 ret=map(f1,li)
 8 #类似于filter,但是区别是map返回值的元素是函数f1的返回值
 9 print(ret)
10 print(list(ret))
11 print(list(ret))
12 
13 执行结果:
14 <map object at 0x102982048>
15 [11, 22, 133, 144, 155]
16 []
map

filter和map的区别:

filter:函数返回True,则元素添加到结果中

map:将函数返回值添加到结果中

  

posted @ 2017-02-21 17:01  meitangyanyan  阅读(190)  评论(0编辑  收藏  举报