一、for 的break python
for x in range(5): print(x) for i in range(5): print('\t %d' % i) if i >3: break
二、for語句的else,for循環在遍歷完列表後,才執行elseapp
for x in range(10): print (x) else: print('#######')
三、判斷是否全爲偶數,return OK異步
def estimate(tuple): for x in list: if x % 2 !=0: break else: print('OK') estimate((2,4,6,8,10,12)) ################################### is_ok = True for x in range(0,10,2): if x %2 != 0: is_ok = False if is_ok: print('OK')
四、列表list的操做async
增:append,extend,insertide
刪:clear,pop,remove函數
改:reverse,sortspa
查:count,index3d
五、切片orm
li[:]協程
li
[1, 5, 7, 3, 4, 7]
id(li)
139848369813128
li1=li[:]至關於li.copy()
id(li1)
139848369542088
li1
[1, 5, 7, 3, 4, 7]
六、打包和解包
x,*_,y,z=(2,3,4,56,7,9)
x,y,z
(2, 7, 9)
x,y=(2,3)
print(x,y)
2 3
x,y=y,x
print(x,y)
3 2
七、集合
n [82]:
s=set((2,3,4,5,5,5,5,5,6))
s
{2, 3, 4, 5, 6}
s.update([2,2,3,4,5,5,5,8,9])
s
{2, 3, 4, 5, 6, 8, 9}
s.discard(10) discard不會拋出異常
s.remove(10)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-87-99f2b84d3df8> in <module>()
----> 1 s.remove(10)
KeyError: 10
八、字典,字典在py3和py2中的方法不一樣
d
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
d.items()
dict_items([('b', 2), ('a', 1), ('c', 3), ('d', 4)])
for x,y in d.items():
print('%s=%s' % (x,y))
b=2
a=1
c=3
d=4
九、參數列表
def add(x,y):
print("x={0}".format(x))
print("y={0}".format(y))格式化
return x+y
add(3,53)位置參數
x=3
y=53
56
add(y=5,x=56)關鍵字參數
x=56
y=5
61
add(4,y=54)位置參數必須放在關鍵字參數的前面
x=4
y=54
58
add(y=34,4)
File "<ipython-input-115-17083eb1d10d>", line 1
add(y=34,4)
^
SyntaxError: non-keyword arg after keyword arg
def sum(lst):
ret=0
for x in lst:
ret+=x
return ret
sum([2,3,4,5,6,1,7,8])
36
def sum(*arg):可變位置參數
ret=0
print(arg)
for x in arg:
ret+=x
return ret
sum(2,3,4,5,6,1,7,8)
(2, 3, 4, 5, 6, 1, 7, 8)
36
def print_info(*args,x,y,**kwargs):可變位置,可變關鍵字,位置參數,關鍵字參數
print('x={0}'.format(x))
print('y={0}'.format(y))
for x in args:
print(x)
for x,y in kwargs.items():
print('{0}={1}'.format(x,y))
print_info(3,1,4,2,x=35,y=5,ab=34,a=4,b=6)
x=35
y=5
3
1
4
2
b=6
ab=34
a=4
def f1(a,b=1,*args): print('a={0}'.format(a)) print('b={0}'.format(b)) for x in args: print(x) f1(1,2,3,5,6,7,8) a=1 b=2 3 5 6 7 8
def f1(a,*args,b=1): print('a={0}'.format(a)) print('b={0}'.format(b)) for x in args: print(x) f1(1,2,3,5,6,7,8,b=2) a=1 b=2 2 3 5 6 7 8
def f1(*args,a,b=1): print('a={0}'.format(a)) print('b={0}'.format(b)) for x in args: print(x) f1(1,2,3,5,6,7,a=8,b=2) a=8 b=2 1 2 3 5 6 7
十、參數解包 def add(x,y): print('x is {0}'.format(x)) print('y is {0}'.format(y)) return x+y lst=[1,4] add(*lst) x is 1 y is 4 5 d={'x':1,'y':6} add(**d) x is 1 y is 6 7
十一、默認參數的坑
def fn(lst1,lst2=[]): for x in lst1: lst2.append(x) print(lst2) fn([1,2,3,4,5]) [1, 2, 3, 4, 5] fn([1,2,3,4,5]) [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
這裏lst2的地址到整個程序執行完成後才釋放
def fn(lst1,lst2=None): if lst2 is None: lst2=[] for x in lst1: lst2.append(x) print(lst2) fn([1,2,3,4,5]) [1, 2, 3, 4, 5] fn([1,2,3,4,5]) [1, 2, 3, 4, 5] fn([1,2,3,4,5],[5,4,3,2,1]) [5, 4, 3, 2, 1, 1, 2, 3, 4, 5]
十二、函數調用過程
重點理解: 一、假設程序是單進程,但執行流,在某一時刻,能運行的程序流只能有一個,但函數調用會打開新的執行上下文,所以,爲了確保main函數能夠恢復現場,在main函數調用其餘函數時,須要先把main函數的現場保存下來,放一邊,即壓棧,這時候,被調用的函數便可執行,且執行完成後,可加到調用者main,回到main函數後,main函數可繼續向後執行。 二、stack保存的是當前執行的函數地址 heap保存的是變量的引用 堆和隊列都是先進後出,棧是先進先出
def add(x,y): return x+y def inc(x,y,z): return add(x,y)+z def main(): x=2 y=3 z=4 ret=inc(x,y,z) print(ret) main() 9
1三、生成器、協程
def iterator(x): i=0 while i<x: yield i i+=1 def main(): for i in iterator(5): print(i) main() 0 1 2 3 4
iterator默認在棧中(只是暫停),不會被銷燬
這就是協程,異步的過程,在用戶空間實現交替,不是在內核空間中,因此執行快,async就是拿yield來實現的
當調用有yield語句的函數時,函數內部的代碼不是當即執行的,而是隻返回一個生成器對象
python2中沒有yield from語句,python3中有
def iterator(list): yield from list def main(): for i in iterator([2,3,4,5,7,7]): print(i)
1四、o(1)的集合,o(n)的列表,空間換時間的方法
li=[2,4,5,7,2,3,7,9,5,8]%注意類比 ret=list() tmp=set() for item in li: if item not in tmp: ret.append(item) tmp.add(item) print(ret) [2, 4, 5, 7, 3, 9, 8] li=[2,4,5,7,2,3,7,9,5,8] tmp=list() for item in li: if item not in tmp: tmp.append(item) print(ret) [2, 4, 5, 7, 3, 9, 8]
1五、利用python找素數=
li=[2,3,4,5,6,7,8,9,10,11,12,13] count=0 for item in li: for i in range(2,item): if item%i==0: break else: count+=1 print(item) print(count)
import math li=[2,3,4,5,6,7,8,9,10,11,12,13] count=0 for item in li: for i in range(2,math.ceil(math.sqrt(item))): if item%i==0: break else: count+=1 print(item) print(count)