一、能夠看成值被賦值給變量python
def func(): print(1) print(func) a = func a()
二、看成元素存放在容器中函數
def func(): print(1) lst = [func,func,func] for i in lst: i()
三、函數名能夠看成函數的參數測試
def func(f): f() def foo(): print(123) func(foo) --------------------- def func(f): f() def foo(): print('is foo') func(foo)
四、函數名能夠看成函數的返回值code
def func(): def foo(): print(123) return foo a = func() a() ----------------------------- def f1(): def f2(): print(1) def f3(): print(2) return f2 ret = f2() return ret print(f1())
python3.6版本以上才能使用orm
s = f"你好{'常鑫'}" s1 = F"你好{'常鑫'}"
s1 = f"你仍是挺好的{s}" print(s1)
s1 = f"{34+11}" print(s1)
a = 10 b = 20 s1 = f"{a if a>b else b}" print(s1)
s1 = f"{{{{{'常鑫'}}}}}" print(s1) -------------------- s1 = f"{'{}{}{}{}{}'}" print(s1)
其餘填充對象
s1 = f"{print(123)}" print(s1) --------------------------- def prin(a): print(a) s1 = f"{prin(123)}" prin(s1) --------------------------- def foo(): def func(): a = 1 return a return func() s1 = f"{foo()}" print(s1) --------------------------- lst = [1,3,4,5,6] s1 = f"{lst[0:5]}" print(s1) # [1,3,4,5,6] --------------------------- dic = {"key1":123,"key2":234} s1 = f"{dic['key2']}" print(s1)
%s,format,f都是格式化,均可實現效果。遞歸
list、dict,str,tuple,set——可迭代對象索引
方法一:內存
list.__iter__() dict.__iter__() str.__iter__() set.__iter__() tuple.__iter__()
方法二:字符串
查看源碼
方法三:
print(dir(list))
**官方聲明只要具備______iter__()方法的就是可迭代對象**
一、使用靈活
二、直接查看值
消耗內存,不能迭代取值
list,tuple,str —— 索引
dict ——鍵
set——直接取值
官方聲明只要具備______iter______()方法和______next______()方法就是迭代器
f = open("xxxx",'w') f.__iter__() f.__iter__()
優勢:節省內存,惰性機制
缺點:使用不靈活,操做比較繁瑣,不能直接查看所有的值
迭代器的特性:
一、一次性的(用完就沒有了)
二、不能逆行(不能後退)
三、惰性機制(節省內存):要一個給一個,不要就不給
當容器中數據量較大的時候使用迭代器
lst = [1,2,3,4,55] new_list = lst.__iter__() # 將可迭代對象轉換成迭代器 ----------------------------- new_list.__iter__() new_list.__next__() ---------------------- s = '123455' new_s = s.__iter__() # 將可迭代對象轉換成迭代器 print(new_s) new_s.__iter__() new_s.__next__() ------------------ new_s = s.__iter__() # 將可迭代對象轉換成迭代器 print(new_s) new_s.__iter__() print(new_s.__next__()) print(new_s.__next__()) print(new_s.__next__())
for循環
s = "12343" # 更改版for的本質 s = [1,2,3,4,5,7] count = len(s) new_s = s.__iter__() while count: print(new_s.__next__()) count -= 1 -------------------------------- s = "12345" new_s = s.__iter__() while True: try: print(new_s.__next__()) # for真實本質 except StopIteration: break ---------------------------- 萬能報錯 # except Exception: print('我是萬能的!') break
有效遞歸:
遞:一直執行直到碰到結束條件
歸:從結束條件開始往回退
官方聲明:最大層次(1000)能夠修改,實際測試(998或者997)
def func(): print(123) func() func() def age(n): # 1,2,3 if n == 3: return "猜對了" else: age(n+1) print(age(1)) def age2(n): if n == 3: return "猜對了" def age1(n): if n == 3: return "猜對了" else:++ age2(n+1) def age(n): if n == 3: return "猜對了" else: age1(n+1) age(1) ----------------------------------- 1.寶元 18-2-2-2 2.太白 18-2-2 3.wusir 18-2 4.alex 18 def age(n): if n == 4: return 18 else: return age(n+1)-2 print(age(1)) def age4(n): if n == 4: return 18 def age3(n): if n == 4: # 問的是否是第四個了 return 18 else: return age4(n+1)-2 def age2(n): if n == 4: # 問的是否是第四個了 return 18 else: return age3(n+1)-2 def age1(n): if n == 4: # 問的是否是第四個了 return 18 else: return age2(n+1)-2 print(age1(1))