day1-6内置函数。

内置函数:

 

 

# all()   如果可迭代对象里面都为真则为True,如果有一个为假就为False
#非0为真,0为假。
a = all([0,5])
print(a)
# any() 如果可迭代对象里面都为假则为False,如果其中一个为真就为True a1 = any([0,6]) print(a1)
#ascii() 将列表变为字符串的形式。 a = ascii([1,2,"开外挂"]) print(type(a)) print([a])
#bin() 十进制转二进制 print(bin(25))
#bool() 判断真假 print(bool()) #False print(bool(0)) #False print(bool(1)) #True print(bool(2)) #True print(bool([])) #False print(bool([1])) #True

#bytearray() 二进制数组 将字符串变为数组的格式,可以修改。 a = bytes("abcde",encoding="utf-8") print(a.capitalize(),a) print(type(a)) #从上面可以看出字符串是不能修改,只能创建一个新的。

#下面通过bytearray() 将字符串变为数组的格式让二进制可以修改。 b = bytearray("abcde",encoding="utf-8") print(type(b)) print(b) print(b[1]) b[1] = 50 print(b)
#callable() 判断是否可以调用,后面可以加上()的就可以调用 可以调用的有函数和类 def test():pass print(callable(test)) #返回True,函数可以调用 print(callable([])) #返回False,列表不可调用

#chr() ascii码表 数字--->字符 print(chr(97)) #返回 a,

#ord() ascii码表 字符---->数字 print(ord("a"))
#classmethod()

#compile() 用于把代码进行编译的过程, 解释:将字符串转换为命令执行 code = "for i in range(10):print(i)" c = compile(code,"","exec") exec(c)
# 或者直接使用exec(code)实现将字符串转换为命令执行。 exec(code) code1 = "1+3/2*6" c1 = compile(code1,"","eval") print(eval(c1))
#delattr() 以后讲

#dict() 生成字典 # 两种字典生成方式: # 1、dict() # 2、{}

# dir() 查看 使用方法即命令。 a = {} print(dir(a))
# divmod() #返回两个数相除的商和余数。 print(divmod(5,2)) #返回(2,1)

# enumerate() 遍历列表 list1=["","","一个","测试"] for index,item in enumerate(list1,1): print(index,item)
#eval() 将字符串变为字典 将加减乘除算法转换为字典。

#exec() 将字符串转换为字典执行,

#filter() def sayhi(n): print(n) sayhi(3)
#匿名函数lambda实现上面sayhi函数的功能。 匿名函数只能实现三元运算。 calc = lambda n:print(n) calc(5)
#在0-10之间将大于5的过滤出来。 res = filter(lambda n:n>5,range(10)) for i in res: print(i)
#map() 将传入的每个值按照一种方式处理。 现在按n*2处理 res1 = map(lambda n:n*2,range(10)) # 等同于 i*2 for i in range(10) for i in res1: print(i)
#reduce() 需要调用functools工具,相加或者相乘等。 #利用reduce,实现0+1+2...+10 import functools res2 = functools.reduce(lambda x,y:x+y,range(10)) print(res2) #返回45 从0加到10,依次相加

#利用reduce,将从1一直乘到10. 1x2x3...x10 res3 = functools.reduce(lambda x,y:x*y,range(1,10)) print(res3)
# float() 转换为浮点数 print(float(10))
# format()

# frozenset() 不可变集合 a集合就有pop功能,a1就没有pop功能了。 a = set([1,2,444,44,55,44,12,4]) a.pop() a1 = frozenset([1,2,444,44,55,44,12,4])
#getattr() 面向对象的时候讲

# globals() 返回当前程序(内置函数1)的全局变量,返回的是字典,key:values # print(globals())

# hasattr() # print(hasattr(alex)) print(hash("alex"))
# hex() 将十进制转换为十六进制 print(hex(16))
# id() 返回内存地址 print(id(10))
# input()

# int()

# isinstance()

# issubclass() 是不是一个子类

# iter() 迭代器

# len() 长度

# list() 列表

# locals() 显示局部变量 def test(): local_var = 333 # print(globals()) print(locals()) test()
# max() 返回列表里面的最大值

# min() 返回列表里面的最小值

# next() 迭代器两个下划线的next __next

# object() 对象 ,python中一切皆对象。

# oct() 转换为八进制 print(oct(8))
# pow() 前面数的后面次方 print(pow(2,8)) #2的8次方。

# property() 以后讲
#
range() 范围
#
repr() 用字符串表示对象。
#
reversed() 反转
#
round() 保留有效数字 print(round(3.14159,2)) #保留两位有效数字
#
setattr() 后面讲
#
slice() 切片 可以忘记
#
sorted() 字典排序。 a = {6:2,8:0,1:4,-5:6,99:11,4:22} print(sorted(a.items())) #默认按照key来排序 print(sorted(a.items(),key=lambda x:x[1])) #按照value来排序。 print(a)

# staticmethod() 后面讲
#
str( )
#
sum() 列表求和
#
super() 以后讲
#
tuple() 元组
#
type() 数据类型
#
vars() 用不到
#
zip() 列表合并 a = [1,2,3,4,5,6] b = ["a","b","c","d"] c = zip(a,b) for i in c: print(i) # __import__() 调用一个字符串 后面会用到 __import__("def")

 

posted on 2017-07-31 22:34  aptech2017  阅读(73)  评论(0)    收藏  举报

导航