python 内置方法

abc(*args, **kwargs)

取绝对值

def add(a,b,f):
    return f(a)+f(b) res = add(3,-6,abs) print(res)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

all(*args, **kwargs)

如果可迭代对象里面所有的元素都为真(非0),返回True
可迭代对象为空,也返回True
print( all([1,-5,3]) ) print( all([0,-5,3]) ) print(all([])) 运行结果: True False True 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

any(*args, **kwargs)

如果可迭代对象里面任意的元素都为真(非0),返回True
可迭代对象为空,也返回False
print( any([]) )
print( any([1,-5,3]) ) print( any([0,-5,3]) ) 运行结果: False True True 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

ascii(*args, **kwargs)

Return an ASCII-only representation of an object.
a= ascii([1,2,"开外挂开外挂"]) print(type(a),[a]) 运行结果: <class 'str'> ["[1, 2, '\\u5f00\\u5916\\u6302\\u5f00\\u5916\\u6302']"] 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

bin(*args, **kwargs)

Return the binary representation of an integer.
十进制转二进制
print(bin(1))

print(bin(3)) print(bin(8)) print(bin(255)) 运行结果: 0b1 0b11 0b1000 0b11111111 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

bool(x)

判断真假
  • 1

bytearray()

字节和字符串不可修改,要修改,创建一个新的,在原有修改会覆盖

bytearray()可修改的字节格式

a = bytes("abcde",encoding="utf-8")
b = bytearray("abcde",encoding="utf-8") print(a.capitalize(),a) print( b[1] ) b[1]= 50 print( b[1] ) print(b) 运行结果: b'Abcde' b'abcde' 98 50 bytearray(b'a2cde') 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

callable()

是否可以调用

def sayhi():pass print( callable(sayhi) ) print( callable([]) ) 运行结果: True False 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

chr(*args, **kwargs)

返回ASCII码对应值
print(chr(98))
print(chr(87)) 运行结果: b W 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

ord()

输入的是ASCII码,与chr() 相反

print(ord('?'))
print(ord('@')) 运行结果: 63 64 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

complie(),exec()

code = '''
def fib(max): #10
    n, a, b = 0, 0, 1 while n < max: #n<10 #print(b) yield b a, b = b, a + b #a = b a =1, b=2, a=b , a=2, # b = a +b b = 2+2 = 4 n = n + 1 return '---done---' g = fib(6) for i in g: print(i) ''' py_obj = compile(code,"err.log","exec") exec(py_obj) exec(code) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

dir()

看字典里有什么方法
>>> a= {}
>>> dir(a)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get attribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__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', 'valu es'] 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

divmod(x, y)

Return the tuple (x//y, x%y) ,(商,余数)

>>> divmod(1,4)
(0, 1) 
  • 1
  • 2
  • 3
  • 4
  • 5

eval(*args, **kwargs)

可以把字符变字典,字符里有控制语句的(如for),就得用exec
a= "{1:'sadas'}"
print(type(eval(a)),eval(a)) x=1 print(eval("x+1+3")) 运行结果: <class 'dict'> {1: 'sadas'} 5 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

filter(function,iterable)

先学习一下 匿名函数

def sayhi(n):
    print(n)

sayhi(5) (lambda n:print(n))(5) calc = lambda n:3 if n<4 else n print(calc(2)) 运行结果: 5 5 3 filter()一组数据中过滤出你想要的 res = filter(lambda n:n>5,range(10)) print(res) #变成一个迭代器 for i in res: print(i) 运行结果: <filter object at 0x0000018F692BB080> 6 7 8 9 map() res = map(lambda n:n*2,range(10)) # [i * 2 for i in range(10)] , res = [ lambda i:i*2 for i in range(10)] print(res) for j in res: print(j) 运行结果: 0 2 4 6 8 10 12 14 16 18 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

frozenset()

>>> set2 = frozenset(set1)
>>> set2
frozenset({3, 4, 5}) >>> set2.remove(2) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'frozenset' object has no attribute 'remove' 不可变集合 a = frozenset([1,4,333,212,33,33,12,4]) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

globals(*args, **kwargs)

返回当前程序所有变量的key,value
Return the dictionary containing the current scope's global variables.

print(globals())

运行结果:
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E64F79B048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/PycharmProjects/python_code_scq/04_week_code/内置方法.py', '__cached__': None, '__author__': 'sunchengquan', '__mail__': '1641562360@qq.com'} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

hex()

转十六进制
>>> hex(2)
'0x2'
>>> hex(10) '0xa' >>> hex(15) '0xf' >>> hex(600) '0x258' 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

id()

返回内存地址
  • 1

locals(*args, **kwargs)

def test():
    local_var =333 print(locals()) print(globals()) test() print(globals()) print(globals().get('local_var')) 运行结果: {'local_var': 333} {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E7C63BB048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/PycharmProjects/python_code_scq/04_week_code/内置方法.py', '__cached__': None, '__author__': 'sunchengquan', '__mail__': '[email protected]', 'test': <function test at 0x000001E7C6013E18>} {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E7C63BB048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/PycharmProjects/python_code_scq/04_week_code/内置方法.py', '__cached__': None, '__author__': 'sunchengquan', '__mail__': '[email protected]', 'test': <function test at 0x000001E7C6013E18>} None 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

oct()

转八进制
>>> oct(7)
'0o7'
>>> oct(8) '0o10' >>> oct(9) '0o11' 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

pow()

Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
>>> pow(3,3) 27 >>> pow(2,3) 8 >>> pow(2,8) 256 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

sorted(*args, **kwargs)

a = {6:2,8:0,1:4,-5:6,99:11,4:22} print(sorted(a)) print(sorted(a.items())) #按key排序 print(sorted(a.items(),key=lambda x:x[1]))# 按value 排序 运行结果: [-5, 1, 4, 6, 8, 99] [(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)] [(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)] 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

zip()

a = [1,2,3,4,5,6] b = ['a','b','c','d'] for i in zip(a,b): print(i) 运行结果: (1, 'a') (2, 'b') (3, 'c') (4, 'd') 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

import()

__import__('decorator')

猜你喜欢

转载自www.cnblogs.com/iexperience/p/9079412.html
今日推荐