python學習day15 day16 內建函式、匿名函式
阿新 • • 發佈:2018-11-02
https://www.processon.com/view/link/5bdc4fe3e4b09ed8b0c75e81
例子:
print(locals()) #返回本地作用域中的所有名字 print(globals()) #返回全域性作用域中的所有名字 global 變數 nonlocal 變數
print(hash(12345)) print(hash('hsgda不想你走,nklgkds')) print(hash(('1','aaa'))) # 雜湊地址 print(hash([])) # 報錯
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print """ print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) file: 預設是輸出到螢幕,如果設定為檔案控制代碼,輸出到檔案 sep: 列印多個值之間的分隔符,預設為空格 end: 每一次列印的結尾,預設為換行符 flush: 立即把內容輸出到流檔案,不作快取 """
# 列印進度條 importtime for i in range(0,101,2): time.sleep(0.1) char_num = i//2 per_str = '\r%s%% : %s\n' % (i, '*'*char_num) \ if i == 100 else '\r%s%% : %s' % (i, '*'*char_num) # \r是回到行首 print(per_str, end=' ', flush=True)
字串型別程式碼的執行
http://www.cnblogs.com/Eva-J/articles/7266087.html
exec和eval都可以執行 字串型別的程式碼
eval有返回值 —— 有結果的簡單計算
exec沒有返回值 —— 簡單流程控制
eval只能用在你明確知道你要執行的程式碼是什麼
code = '''for i in range(10): print(i*'*') ''' exec(code) code1 = 'for i in range(0,10): print (i)' compile1 = compile(code1,'','exec') exec(compile1) code2 = '1 + 2 + 3 + 4' compile2 = compile(code2,'','eval') print(eval(compile2)) code3 = 'name = input("please input your name:")' compile3 = compile(code3,'','single') exec(compile3) # please input your name: 執行時顯示互動命令,提示輸入 print(name) #
sum接收的是可迭代物件
ret = sum([1,2,3,4,5,6]) # 接收的是可迭代的物件 print(ret) ret = sum([1,2,3,4,5,6,10],10) # 初始加10 print(ret)
print(max([1,2,3,4])) # 4 print(max(1,2,3,-4)) print(max(1,2,3,-4,key = abs)) # -4 以絕對值論最大值