博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python_69_内置函数1
阅读量:4552 次
发布时间:2019-06-08

本文共 2966 字,大约阅读时间需要 9 分钟。

#abs()取绝对值'''all(iterable)Return True if all elements of the iterable are true (or if the iterable is empty).'''print(all([0,9,-8,'a']))print(all([9,-8,'a']))'''any(iterable)Return True if any element of the iterable is true. If the iterable is empty, return False.'''print(any([0,9,-8,'a']))print(any([]))# ascii(object)# As repr(), return a string containing a printable representation of an object,# but escape the non-ASCII characters in the string returned by repr() using \x, \u or \U escapes.a=ascii(['a',1,'汉子'])print(a,[a])print(type(a))#格式是字符串'''bin(x)Convert an integer number to a binary string prefixed with “0b”. '''print(bin(3))#布尔boolprint(bool(0))print(bool(1))print(bool([]))#bytea=bytes('abc',encoding='utf-8')print(a.capitalize(),a)#字符串不可以修改,二进制的字节格式也不可以修改,要想修改只能生成新的#bytearray字节的数组可以被修改b=bytearray('abc',encoding='utf-8')print(b[0],b[2])#b[0]='B'#错误。必须以字节形式修改b[0]=65b[1]=66b[2]=67print(b)'''callable(object)判断是否可以被调用'''print(callable([]))def diaoyong():passprint(callable(diaoyong))#函数和类都可调用# chr(i),i必须是数字,将数字转为ascll码字符print(chr(67))#ord与chr相反print(ord('C'))#compile()code1='for i in range(10):print(i)'exec(code1)#以下两行程序同等此行# c=compile(code1,'','exec')#exec将字符串编码可执行的程序# exec(c)#print(c),c是内存中的数据对象code2='1+3/2*6'print(eval(code2)) #以下两行程序同等此行'''c=compile(code2,'','eval')eval(c)#,适合字符串变字典,以及加减乘除类,不适合语句类,比如for循环,这样的用exec'''code3='''import timedef consumer(name):    print("%s 准备吃包子啦!" %name)    while True:       baozi = yield       print("包子[%s]来了,被[%s]吃了!" %(baozi,name))c=consumer('猪小芳')c.__next__()b1='韭菜馅''''exec(code3)#以下两行程序同等此行# py_obj=compile(code3,'err.log','exec')#编译过程中出的错会写到err.log中,写不写无所谓,不好使# exec(py_obj)#complex复数print(complex('1+2j')+complex('2+2j'))#dict字典print(dict())#生成字典#dir查看使用方法a={}b=()print(dir(a))print(dir(b))#divmod(a,b),return商和余数print(divmod(6,4))#匿名函数,用完就释放(lambda n:print(n*n))(5)#一种传递参数的方法calc=lambda n:print(n*n)calc(10)# calc2=lambda x:for i in range(x):print(i)#处理不了复杂的,可以处理三元运算这种简单的calc3=lambda n:3 if n<4 else nprint(calc3(5))#lambda可与filter过滤器结合使用res=filter(lambda n:n>5,range(10))print(res)#迭代器for i in res:    print(i,end='\t')print('>>>>>>>>>分隔符1')#lambda可与map结合使用res=map(lambda n:n*2,range(10))#等价于[i*2 for i in range(10)],[lambda n:n*2 for i in range(10)]print(res)#迭代器for i in res:    print(i,end='\t')print('>>>>>>>>>分隔符1')import functoolsprint(functools.reduce(lambda x,y:x*y,range(1,10)))#阶乘print(functools.reduce(lambda x,y:x+y,range(10)))#累加和#不可变集合,就和元组似的a=frozenset([1,4,333,212,33,33,12,4])print(a)#返回本文件程序的所有全局变量及值print(globals())'''hash(object)哈希:每个数据对应一个数字映射,便于查找Return the hash value of the object (if it has one).Hash values are integers.They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).'''print(hash('熊羚羽'))print(hash('韩江桦'))print(hash('俞莎莎'))print(hash('熊羚羽'))#与第一个对应的映射是相同的

 

转载于:https://www.cnblogs.com/tianqizhi/p/8402069.html

你可能感兴趣的文章
网络体系结构
查看>>
练习4.13、4.14、4.15、4.16
查看>>
根据数据库连接,登录操作系统的一个方法
查看>>
yii 常用的多表查询
查看>>
带符号的整数做减法
查看>>
Cmder之vim配置
查看>>
SAP库龄表
查看>>
tomcat加密
查看>>
WebDriver的工作原理
查看>>
js call 理解
查看>>
ES6笔记01
查看>>
凯撒密码、GDP格式化输出、99乘法表
查看>>
linux下获取外网IP
查看>>
引用阿里巴巴图标库
查看>>
【学步者日记】实现破碎效果 Fracturing & Destruction 插件使用
查看>>
迷宫全解
查看>>
flex布局,如果其中一个过宽,会影响另个一的
查看>>
js---加入收藏夹
查看>>
泛型的优点
查看>>
一个研究生毕业以后的人生规划
查看>>