内置函数:

 

  1 # all()   如果可迭代对象里面都为真则为True,如果有一个为假就为False
  2 #非0为真,0为假。
  3 a = all([0,5])
  4 print(a)
  5 
  6 # any()   如果可迭代对象里面都为假则为False,如果其中一个为真就为True
  7 a1 = any([0,6])
  8 print(a1)
  9 
 10 #ascii()   将列表变为字符串的形式。
 11 a = ascii([1,2,"开外挂"])
 12 print(type(a))
 13 print([a])
 14 
 15 #bin()   十进制转二进制
 16 print(bin(25))
 17 
 18 #bool()  判断真假
 19 print(bool())           #False
 20 print(bool(0))          #False
 21 print(bool(1))          #True
 22 print(bool(2))          #True
 23 print(bool([]))         #False
 24 print(bool([1]))        #True
 25 
 26 
 27 #bytearray()       二进制数组  将字符串变为数组的格式,可以修改。
 28 a = bytes("abcde",encoding="utf-8")
 29 print(a.capitalize(),a)
 30 print(type(a))
 31 #从上面可以看出字符串是不能修改,只能创建一个新的。
 32 
 33 
 34 #下面通过bytearray() 将字符串变为数组的格式让二进制可以修改。
 35 b = bytearray("abcde",encoding="utf-8")
 36 print(type(b))
 37 print(b)
 38 print(b[1])
 39 b[1] = 50
 40 print(b)
 41 
 42 #callable()  判断是否可以调用,后面可以加上()的就可以调用  可以调用的有函数和类
 43 def test():pass
 44 print(callable(test))        #返回True,函数可以调用
 45 print(callable([]))          #返回False,列表不可调用
 46 
 47 
 48 #chr()   ascii码表  数字--->字符
 49 print(chr(97))      #返回 a,
 50 
 51 
 52 #ord()    ascii码表   字符---->数字
 53 print(ord("a"))
 54 
 55 #classmethod()
 56 
 57 
 58 #compile()    用于把代码进行编译的过程,  解释:将字符串转换为命令执行
 59 code = "for i in range(10):print(i)"
 60 c = compile(code,"","exec")
 61 exec(c)
 62 
 63 # 或者直接使用exec(code)实现将字符串转换为命令执行。
 64 exec(code)
 65 code1 = "1+3/2*6"
 66 c1 = compile(code1,"","eval")
 67 print(eval(c1))
 68 
 69 #delattr()  以后讲
 70 
 71 
 72 #dict()    生成字典
 73 # 两种字典生成方式:
 74 #                  1、dict()
 75 #                  2、{}
 76 
 77 
 78 # dir()    查看 使用方法即命令。
 79 a = {}
 80 print(dir(a))
 81 
 82 # divmod()      #返回两个数相除的商和余数。
 83 print(divmod(5,2))     #返回(2,1)
 84 
 85 
 86 # enumerate()     遍历列表
 87 list1=["","","一个","测试"]
 88 for index,item in enumerate(list1,1):
 89     print(index,item)
 90 
 91 #eval()  将字符串变为字典  将加减乘除算法转换为字典。
 92 
 93 
 94 #exec()   将字符串转换为字典执行,
 95 
 96 
 97 #filter()
 98 def sayhi(n):
 99     print(n)
100 sayhi(3)
101 
102 #匿名函数lambda实现上面sayhi函数的功能。  匿名函数只能实现三元运算。
103 calc = lambda n:print(n)
104 calc(5)
105 
106 #在0-10之间将大于5的过滤出来。
107 res = filter(lambda n:n>5,range(10))
108 for i in res:
109     print(i)
110 
111 #map()  将传入的每个值按照一种方式处理。 现在按n*2处理
112 res1 = map(lambda n:n*2,range(10))     # 等同于 i*2 for i in range(10)
113 for i in res1:
114     print(i)
115 
116 #reduce()  需要调用functools工具,相加或者相乘等。
117 #利用reduce,实现0+1+2...+10
118 import functools
119 res2 = functools.reduce(lambda x,y:x+y,range(10))
120 print(res2)      #返回45 从0加到10,依次相加
121 
122 
123 #利用reduce,将从1一直乘到10. 1x2x3...x10
124 res3 = functools.reduce(lambda x,y:x*y,range(1,10))
125 print(res3)
126 
127 # float()   转换为浮点数
128 print(float(10))
129 
130 # format()
131 
132 
133 # frozenset()  不可变集合  a集合就有pop功能,a1就没有pop功能了。
134 a = set([1,2,444,44,55,44,12,4])
135 a.pop()
136 a1 = frozenset([1,2,444,44,55,44,12,4])
137 
138 #getattr()   面向对象的时候讲
139 
140 
141 # globals()   返回当前程序(内置函数1)的全局变量,返回的是字典,key:values
142 # print(globals())
143 
144 
145 # hasattr()
146 # print(hasattr(alex))
147 print(hash("alex"))
148 
149 # hex()    将十进制转换为十六进制
150 print(hex(16))
151 
152 # id()   返回内存地址
153 print(id(10))
154 
155 # input()
156 
157 
158 # int()
159 
160 
161 # isinstance()
162 
163 
164 # issubclass()  是不是一个子类
165 
166 
167 # iter()   迭代器
168 
169 
170 # len()   长度
171 
172 
173 # list()   列表
174 
175 
176 # locals()     显示局部变量
177 def test():
178     local_var = 333
179 # print(globals())
180     print(locals())
181 test()
182 
183 # max()     返回列表里面的最大值
184 
185 
186 # min()      返回列表里面的最小值
187 
188 
189 # next()     迭代器两个下划线的next  __next
190 
191 
192 # object()     对象 ,python中一切皆对象。
193 
194 
195 # oct()        转换为八进制
196 print(oct(8))
197 
198 # pow()    前面数的后面次方
199 print(pow(2,8))    #2的8次方。
200 
201 
202 # property()   以后讲
203 
204 # range()   范围
205 
206 # repr()    用字符串表示对象。
207 
208 # reversed()   反转
209 
210 # round()    保留有效数字
211 print(round(3.14159,2))   #保留两位有效数字
212 
213 # setattr()   后面讲
214 
215 # slice()    切片 可以忘记
216 
217 # sorted()      字典排序。
218 a = {6:2,8:0,1:4,-5:6,99:11,4:22}
219 print(sorted(a.items()))    #默认按照key来排序
220 print(sorted(a.items(),key=lambda x:x[1]))   #按照value来排序。
221 print(a)
222 
223 
224 # staticmethod()   后面讲
225 
226 # str( )
227 
228 # sum()     列表求和
229 
230 # super()    以后讲
231 
232 # tuple()      元组
233 
234 # type()       数据类型
235 
236 # vars()     用不到
237 
238 # zip()     列表合并
239 a = [1,2,3,4,5,6]
240 b = ["a","b","c","d"]
241 c = zip(a,b)
242 for i in c:
243     print(i)
244 # __import__()  调用一个字符串   后面会用到
245 __import__("def")

 

 posted on 2018-08-05 21:28  二十二a  阅读(184)  评论(0编辑  收藏  举报