print(dir(__builtins__))
print(len(dir(__builtins__))) # 153個
ps : 工廠函數 : int float str list tuple dict set bool byteshtml
👉部分經常使用內置函數在這👈: max min sorted map reduce filterpython
print(abs(-5)) # 變成正數
print(all([1,'a',0])) # False print(all([1,2,[]])) # False
print(all('')) # True print(all([])) # True
print(any([0,'',None,1])) # True
print(any([])) # False print(any('')) # False
print(bin(11)) # 10--->2 print(oct(11)) # 10--->8 print(hex(11)) # 10--->16
print(bool(0)) #0, None,空的布爾值爲假
res = '你好songhaiixng'.encode('utf-8') print(res) # b'\xe4\xbd\xa0\xe5\xa5\xbdsonghaiixng' res = bytes('你好songhaixing',encoding='utf-8') print(res) # b'\xe4\xbd\xa0\xe5\xa5\xbdsonghaixing'
def func(): pass print(callable(func)) # True print(callable('ss'.strip)) # True print(callable('dac'.split)) # False print(callable(123)) # False
print(chr(90)) #按照表十進制轉化成字符 print(ord('x')) #按照表十進制轉化成數字
print(dir('song')) # [打印出一堆的調用方法]
print(divmod(10,3)) #(3,1)--->10除於3餘1 print(divmod(10,2)) #(5,0)--->10除於2餘0
🍔數學表達式 res = eval('2**3') print(res) # 8 🍔列表 res = eval('[1,2,3,4.5,5]') print(res) # [1, 2, 3, 4.5, 5] print(res[2]) # 3 🍔字典 res = eval("{'name':'shawn','age':18,'sex':'man'}") print(res) # {'name': 'shawn', 'age': 18, 'sex': 'man'} print(res["name"]) # shawn
with open(r"aaa.txt","at",encoding="utf-8")as f: f.write('{"name":"shawn","age":10,"sex":"man"}\n') f.write('{"name":"song","age":18,"sex":"woman"}\n') f.write('{"name":"xing","age":14,"sex":"man"}\n') l = [] with open(r"aaa.txt","rt",encoding="utf-8")as f: for i in f: line = i.strip('\n') res = eval(line) l.append(res) print(l) ''' [{'name': 'shawn', 'age': 10, 'sex': 'man'},\ {'name': 'song', 'age': 18, 'sex': 'woman'},\ {'name': 'xing', 'age': 14, 'sex': 'man'}] ''' print(type(l)) # <class dict> print(l[0]["name"]) # shawn
fset = frozenset({1,2,3}) print(fset) # frozenset({1, 2, 3}) print(type(fset)) # <class 'frozenset'>
🍔當都處於全局名稱空間調用查看時,查看的效果是同樣的 x = 111111 print(globals()) # [一堆綁定關係] print(locals()) # [一堆綁定關係] 🍔當在函數內部使用 "locals()" 查看時, 獲得的就是函數內部名稱空間的綁定關係 def aa(): x = 111 y = 222 print(locals()) aa() #{'x': 111, 'y': 222}
def func(): """ 幫助信息 :return: """ pass print(help(func)) # [顯示裏面的幫助文檔] print(help(max))
print(len({'x':1,'y':2})) # 2 print({'x':1,'y':2}.__len__) # 2
res = iter('song') print(next(res)) # s print(next(res)) # o print(next(res)) # n .......
print(pow(10, 2)) # 100 10**2------>100 print(pow(2,3,3)) # 2 2**3 % 3--->餘 2 print(pow(10, 2, 3)) # 1 10**2 % 3-->餘 1
🍔建立切片對象 res = slice(1, 5, 2) 🍔直接使用建立好的來切 l = [1, 2, 3, 4, 5, 6, 7, 8] print(l[res]) #[2, 4] t = (5, 14, 7, 8, 9, 5, 4, 2) print(t[res]) #(14, 8)
print(sum([1,2,3,4,5])) # 15
left = 'hello' right = (1,2,3,) res = zip(left,right) print(res) # <zip object at 0x0000016FC23A0748> print(type(res)) # <class 'zip'> print(list(res)) # [('h', 1), ('e', 2), ('l', 3)]
with open('m',mode='rt',encoding='utf-8') as f: module=f.read() # 這裏拿到的是字符串 time=__import__(module) # __import__("time") 以字符串形式導入模塊 time.sleep(3) print(time.time())
res = reversed((2,3,65,765,23,20)) res2 = reversed([4,6,2,7,9,0,3,1]) print(tuple(res)) # (20, 23, 765, 65, 3, 2) print(list(res2)) # [1, 3, 0, 9, 7, 2, 6, 4]
print(hash(1231)) # 1231 print(hash("派大星")) # 4444534694763505378 print(hash((1,2,3,4,"a"))) # 4233406373922709203 print(hash(2.5)) # 1152921504606846978 print(hash(['海綿寶寶',12])) # 報錯 : TypeError