1、这里是所有的内置方法的使用方法
# -*- coding:utf-8 -*-# Author : 何子辰# 所有的内置方法总结print('1.abs'.center(50,'*'))# abs 绝对值a = abs(-5)print(a)print('2.all'.center(50,'*'))# all# Return True if all elements of the# iterable are true(or if the iterable# is empty).# 非0即真print(all([0,-5,3]))print('3.any'.center(50,'*'))# any# Return True if any element of the iterable# is true. if the iterable is empty.Return# Falseprint(any([1,-2,9]))print(any([]))print('4.ascii'.center(50,'*'))# ascii# return a string containing a printable of# an objectprint(ascii([1,2,"你好"]))print(type(ascii([1,2,"你好"])))#a = ascii([1,2])print([a])# ['[1, 2]']print('5.bin'.center(50,'*'))# bin# 十进制转换二进制print(bin(255))print('5.bool'.center(50,'*'))# bool# Ture or falseprint(bool(0))print(bool(1))print(bool([]))print('6.bytearray'.center(50,'*'))# bytearray# Return a byte array# bytearray 可修改二进制字节格式a = bytes("abcde",encoding="utf-8")print(a)# b'abcde'print(a.capitalize(),a)# b'Abcde' b'abcde' 字符串不能修改,二进制字节更不能修改b = bytearray("abcde",encoding="utf-8")print(b[0]) # 97 a的ascii码print(b[1])b[1] = 100print(b) # bytearray(b'adcde') b变成了dprint('7.callable'.center(50,'*'))# Judge an object if it can be callableprint(callable([])) # 列表不能调用 *列表不能加括号def sayhi():
passprint(callable(sayhi)) # 函数可以调用print('8.chr & ord'.center(50,'*'))# 返回ascii码的对应表print(chr(89)) #Yprint(chr(99)) #c# 返回字符的ascii码print(ord('b')) #98print('9.classmethod'.center(50,'*'))# 类方法print('10.compile'.center(50,'*'))# 底层代码编译过程# 将字符串编译成可执行代码# code = "for i in range(10): print(i)"# compile(code,'','exec')# >>> code = "for i in range(10):print(i)"# >>> code# 'for i in range(10):print(i)'# >>># >>> compile(code,'','exec')#
at 0x00000000032E9F60, file "", line 1># >>> c = compile(code,'','exec') # 执行器 exec# >>> exec(c)# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# >>># >>> code = "1+3/6"# >>> c = compile(code,'','eval') # 执行器eval# >>> eval(c)# 1.5# 等价于import一个模块 实现了动态导入code = '''def Fibonaci(max):
n,a,b = 0,0,1
while (n < max):
print(b)
a,b = b,a + b
# 不相当于 a = b; b = a+b
# 相当于 t = (b,a+b)
# a = t[0]
# b = t[1]
n = n+1
return 'done'Fibonaci(20)'''py_obj = compile(code,"err.log","exec") # 编译exec(py_obj)# exec(code) # 妈蛋的直接用exec就可以执行....print('11.dir'.center(50,'*'))# dir# 查看一个object有哪些方法b = {}print(dir(b))print('12.divmod'.center(50,'*'))# 相除并返回余数# >>> divmod(5,2)# (2, 1)# >>> divmod(123,3)# (41, 0)# >>>print('13.eval'.center(50,'*'))# 简单数据运算x = 1print(eval('x+1'))print('14.exec'.center(50,'*'))code = '''def Fibonaci(max):
n,a,b = 0,0,1
while (n < max):
print(b)
a,b = b,a + b
# 不相当于 a = b; b = a+b
# 相当于 t = (b,a+b)
# a = t[0]
# b = t[1]
n = n+1
return 'done''''exec(code)# 首先关于匿名函数def sayhi(n):
print(n)sayhi(3)# 转成匿名函数# 匿名函数 处理简单语句# 传参数(lambda n:print(n))(5)calc = lambda n:print(n)calc(5)# 三元运算calc = lambda n:3 if n<4 else nprint(calc(2))print('14.filter'.center(50,'*'))# filter# Function: 一组数据中过滤出用户想要的res = filter(lambda n:n>5,range(10))# 此时变成迭代器,需要循环打印for i in res:
print(i)print('15.map'.center(50,'*'))# map# map对传入的每一个值,按照func的形式处理res1 = map(lambda n:n*n,range(10))# [i*2 for i in range(10)]for i in res1:
print(i)res2 = [ lambda i2:i2*2 for i2 in range(10) ]for i in res2:
print(i)import functools # 2.7还是内置方法print('16.reduce'.center(50,'*'))# reduceres = functools.reduce(lambda x,y:x+y,range(10))# 累加器,从1加到10print(res)res2 = functools.reduce(lambda x,y:x*y,range(1,10))print(res2) #362880 累乘print('17.frozenset'.center(50,'*'))# frozenset# 集合冻结# 集合不可变a = frozenset([1,4,333,22,112,345,551,2,34])print('18.globles'.center(50,'*'))print(globals())# 打印的是当前所有程序的keyvalue格式print('19.hash'.center(50,'*'))# hash# 散列转为有序 1 2 3 4 5# 固定的映射关系print(hash("哈哈哈"))#548739204797215059 固定对应关系print('20.hex'.center(50,'*'))# 转16进制print(hex(255))#0xffprint('21.local'.center(50,'*'))# local# 局部变量#def test():
local_var=333
print(locals())
print(globals())
print(globals().get('local_var'))test()print(globals())print(globals().get('local_var'))print('22.oct'.center(50,'*'))# 转8进制print(oct(1),oct(19),oct(8))#0o1 0o23 0o10print('23.pow'.center(50,'*'))# pow(x,y) x**y x的y#print(pow(3,5))#243print('24.round'.center(50,'*'))# round(float,N) 浮点数保留N位小数点print(round(1.3342,3))# 1.334print('25.slice'.center(50,'*'))d = range(20)slice(1,6,None)k = d[slice(1,6,None)]for i in k:
print(i)# Output
1#
2#
3#
4#
5print('26.sorted'.center(50,'*'))a = {1:'alex',2:'June',3:'Allen',7:'Cook',10:'alex',
17:'Yunny'}print(a)# {1: 'alex', 2: 'June', 3: 'Allen', 17: 'Yunny', 7: 'Cook', 10: 'alex'}# 字典的无序性# 给字典排序print(sorted(a))# out:[1, 2, 3, 7, 10, 17]发现排的是key值# 列表是有序的# 用items 将其key值对应的值取出print(sorted(a.items()))# [(1, 'alex'), (2, 'June'), (3, 'Allen'),# (7, 'Cook'), (10, 'alex'), (17, 'Yunny')]# 按value来排序b = {1:12,2:50,3:47,7:58,10:96,17:99}print(sorted(b.items(),key = lambda x:x[1]))# output[(1, 12), (3, 47), (2, 50), (7, 58),# (10, 96), (17, 99)]print('27.vars'.center(50,'*'))# 返回对象所有的属性名print('28.zip'.center(50,'*'))a = [1,2,3,4]b = ['a','b','c','d']# 列表a 和 b组合zip(a,b)print(zip(a,b))#迭代器!for i in zip(a,b):
print(i)#
(1, 'a')# (2, 'b')# (3, 'c')# (4, 'd')# a多 b少a = [1,2,3,4,5,6]b = ['a','b','c','d']# 列表a 和 b组合zip(a,b)print(zip(a,b))#迭代器!for i in zip(a,b):
print(i)# (1, 'a')# (2, 'b')# (3, 'c')# (4, 'd')print('29.import'.center(50,'*'))# 只知道字符串的导入__import__('time')
2.上述程序的执行结果
F:\Python3.4\python.exe F:/PyCharm5/Code/day21/内置函数.py**********************1.abs***********************5**********************2.all***********************False**********************3.any***********************TrueFalse*********************4.ascii**********************[1, 2, '\u4f60\u597d']['[1, 2]']**********************5.bin***********************0b11111111**********************5.bool**********************FalseTrueFalse*******************6.bytearray********************b'abcde'b'Abcde' b'abcde'9798bytearray(b'adcde')********************7.callable********************FalseTrue*******************8.chr & ord********************Yc98******************9.classmethod***************************************10.compile********************11235813213455891442333776109871597258441816765**********************11.dir**********************['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']********************12.divmod******************************************13.eval**********************2*********************14.exec**********************3553********************14.filter*********************6789**********************15.map**********************0149162536496481 . at 0x00000000035ECF28> . at 0x000000000360E048> . at 0x000000000360E0D0> . at 0x000000000360E158> . at 0x000000000360E1E0> . at 0x000000000360E268> . at 0x000000000360E2F0> . at 0x000000000360E378> . at 0x000000000360E400> . at 0x000000000360E488>********************16.reduce*********************45362880*******************17.frozenset***************************************18.globles********************{
'res': 45, '__cached__': None, '__package__': None, '__builtins__':, 'a': frozenset({1, 2, 34, 4, 551, 333, 112, 22, 345}), 'code': "\ndef Fibonaci(max):\n
n,a,b = 0,0,1\n
while (n < max):\n
print(b)\n
a,b = b,a + b\n
# 不相当于 a = b; b = a+b\n
# 相当于 t = (b,a+b)\n
# a = t[0]\n
# b = t[1]\n
n = n+1\n
return 'done'\n", '__doc__': None, '__spec__': None, 'res1':