Python 내장 함수(Built-in Function)

16040 단어
인코딩과 예제를 직접 보려면 다음과 같이 하십시오.
  1 """
  2          Built-in Function
  3 """
  4 
  5 # abs()     
  6 print(abs(-1))
  7 
  8 # all()          bool        0  False
  9 """
 10 Return True if bool(x) is True for all values x in the iterable.
 11 If the iterable is empty, return True.
 12 """
 13 print(all([0, '', None, 1, '1', 'a']))
 14 print(all(''))
 15 
 16 # any()          True
 17 print(any([0, '', None, 1, '1', 'a']))
 18 
 19 # bin()           0b      0b11
 20 print(bin(3))
 21 
 22 # bool()   bool  0 None ''  False
 23 print(bool(None))
 24 print(bool(0))
 25 print(bool(''))
 26 
 27 # bytes()             b      
 28 # utf-8     3    b'\xe4\xbd\xa0\xe5\xa5\xbd'
 29 # gbk     2    b'\xc4\xe3\xba\xc3'
 30 # ascii       
 31 # decode            
 32 print(bytes('  ', encoding='utf-8'))
 33 print(bytes('  ', encoding='utf-8').decode('utf-8'))
 34 print(bytes('  ', encoding='gbk'))
 35 # print(bytes('  ', encoding='ascii'))
 36 
 37 # chr() ASCII   A a
 38 # ord()   
 39 print(chr(65))
 40 print(chr(97))
 41 print(ord('a'))
 42 
 43 # dir()         
 44 print(dir(all))
 45 
 46 # divmod()        ( ,  )     
 47 print(divmod(10, 3))
 48 
 49 # eval()           
 50 #         
 51 dic = {'name': 'louis'}
 52 dic_str = str(dic)
 53 print(eval(dic_str))
 54 print(eval('1 + 2 + 3'))
 55 
 56 # hash()    hash             ,         
 57 # hash          
 58 name = 'louis'
 59 print(hash(name))
 60 print(hash(name))
 61 name = 'scar'
 62 print(hash(name))
 63 
 64 # hex()          
 65 # 0x       
 66 # oct()      8  
 67 # 0o     
 68 print(bin(10))
 69 print(hex(12))
 70 print(oct(12))
 71 
 72 # globals()       
 73 # locals()       
 74 print(globals())
 75 print(locals())
 76 
 77 # zip()                   
 78 #          
 79 people = {'louis': '25', 'scar': '24'}
 80 print(max(zip(people.values(), people.keys())))  # ('25', 'louis')
 81 print(list(zip('abc', '123')))  # [('a', '1'), ('b', '2'), ('c', '3')]
 82 
 83 people_dic = [{'name': 'louis', 'age': 25},
 84               {'name': 'scar', 'age': 24}]
 85 print(max(people_dic, key=lambda p: p['age']))
 86 
 87 # pow()     2**3        
 88 # Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
 89 print(pow(2, 3))
 90 print(pow(2, 3, 2))
 91 
 92 # slice()          
 93 str_hello = 'hello'
 94 s1 = slice(3, 5)  # lo
 95 s2 = slice(1, 4, 2)  # el start stop step
 96 print(str_hello[s1])
 97 print(str_hello[s2])
 98 
 99 # __import__()     
100 # import 'test'    
101 module_name = 'test'
102 m = __import__(module_name)

좋은 웹페이지 즐겨찾기