标签:
一、内置函数
1.chr() ord()
r= chr(65) #ASCII对应的字母 print (r) n= ord("a") #ASCII对应的数字 print(n) #以下为执行结果 A 97
可以利用此函数随机生成验证码:
import random li=[] for i in range(6): r = random.randrange(0,5) if r ==2 or r==4: num = random.randrange(0,10) li.append(str(num)) else: temp = random.randrange(65,91) c = chr(temp) li.append(c) result ="".join(li) print (result) #以下为执行多次的结果 DR2B5G 59BY9F 6EQ8ZP
2.compile() eval() exec()
s = "print (123)" #将字符串编译成python代码 r =compile(s,"<string>","exec") #执行python代码 没有返回值 exec(r) s = "8*8" #只能执行表达式 有返回值 ret = eval(s) print (ret) #以下为执行行结果 123 64
3.dir()
print (dir(list))#快速查看 对象提供了哪些功能呢 #以下为执行结果: [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
4.divmod()
r =divmod(97,10) #除法余数
print (r)
#以下为执行结果:
(9, 7)
5.isinstance()
s = "alex" r = isinstance(s,str) #用于判断对象是否是类的实例 print(r) #以下为执行结果: True
6.filter()
#filter (函数,可迭代的对象) def f2(a): if a>22: return True li = [11,22,33,44,55] ret = filter(f2,li) #filer,迭代第二参数,让每个元素执行函数,函数返回值为True,表示元素合法 print (list(ret)) #以下为执行结果 [33, 44, 55]
以下为 lambda 与 filter函数结合使用:
li = [11,22,33,44,55] result = filter(lambda a:a>33,li) print (list(result)) #以下为执行结果 [44, 55]
7.map()
#map(函数,可迭代的对象) def f2(a): return a+ 100 li = [11,22,33,44,55] result = map(f2,li) print (list(result)) #以下为执行结果: [111, 122, 133, 144, 155]
以下为 lambda 与map函数结合使用
li = [11,22,33,44,55] result = map(lambda a:a+100,li) print (list(result)) #以下为执行结果: [111, 122, 133, 144, 155]
8.locals() globals()
NAME ="ALEX" def show(): a = 123 c = 123 print (locals()) #局域变量 print(globals()) #全局变量 show() #以下为执行结果 {‘c‘: 123, ‘a‘: 123} {‘__doc__‘: None, ‘__package__‘: None, ‘__cached__‘: None, ‘__name__‘: ‘__main__‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x7f04b77e8da0>, ‘__spec__‘: None, ‘NAME‘: ‘ALEX‘, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘show‘: <function show at 0x7f04b77e6158>, ‘__file__‘: ‘a.py‘}
9.hash()
s = "hahah" print (hash(s)) #哈希值 #以下为执行结果: -8346297972582505313
10.len() bytes()
# python3 len判断是字符个数 python2是字节个数 s = "李杰" print(len(s)) #python3查询字节个数可以用 bytes() s = "李杰" b = bytes(s,encoding=‘utf-8‘) print (len(b)) #以下为执行结果 2 6
11.max() min() sum
r = max([1,2,3,4]) #最大值 print (r) #以下为执行结果: 4 r = min([1,2,3,4]) #最小值 print (r) #以下为执行结果: 1 r = sum([1,2,3,4]) #求和 print (r) #以下为执行结果: 10
12. round()
print (round(1.4)) #四舍五入 以下为执行结果: 1
13.sortd()
li =[11,22,3,4] print (sorted(li)) #不改变原列表排序 li.sort() #对原列表进行排序 print (li) #以下为执行结果: [3, 4, 11, 22] [3, 4, 11, 22]
14.zip()
l1=["alex",11,22,33] l2=["aaa",11,22,33] l3 =["bbb",11,22,33] r = zip(l1,l2,l3) #zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表 print (list(r)) #以下为执行结果: [(‘alex‘, ‘aaa‘, ‘bbb‘), (11, 11, 11), (22, 22, 22), (33, 33, 33)] l1=["alex",11,22,33] l2=["aaa",11,22,33] l3 =["bbb",11,22,33] r = zip(l1,l2,l3) #print (list(r)) temp = list(r)[0] ret = ‘ ‘.join(temp) print(ret) #以下为执行结果: alex aaa bbb
二、装饰器
def outer(func): def inner(): print (‘before‘) r = func() print ("after") return r return inner() @outer def f1(): print ("F1") return "aaa" print (f1) #以下为执行结果: before F1 after aaa
def outer(func): def inner(a): print (‘before‘) r = func(a) print ("after") return r return inner @outer def f1(arg): print ("F1") return "aaa" ret = f1("wudalang") print (ret) #以下为执行结果: before F1 after aaa
def outer(func): def inner(*args,**kwargs): print (‘before‘) r = func(*args,**kwargs) print ("after") return r return inner @outer def f1(arg): print("F1") return "kaka" @outer def f2(arg1,arg2): print ("F2") return "aaa" @outer def f3(): print ("F3") ret = f1("fafafa") print ("返回值",ret) ret1 = f2(11,22) print ("返回值",ret1) #以下为执行结果: before F1 after 返回值 kaka before F2 after 返回值 aaa
标签:
原文地址:http://www.cnblogs.com/wudalang/p/5542571.html