1、for 的break
for x in range(5): print(x) for i in range(5): print(‘\t %d‘ % i) if i >3: break
2、for语句的else,for循环在遍历完列表后,才执行else
for x in range(10): print (x) else: print(‘#######‘)
3、判断是否全为偶数,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‘)
4、列表list的操作
增:append,extend,insert
删:clear,pop,remove
改:reverse,sort
查:count,index
5、切片
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]
6、打包和解包
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
7、集合
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
8、字典,字典在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
9、参数列表
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
10、参数解包 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
11、默认参数的坑
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]
12、函数调用过程
重点理解: 1、假设程序是单进程,但执行流,在某一时刻,能运行的程序流只能有一个,但函数调用会打开新的执行上下文,因此,为了确保main函数可以恢复现场,在main函数调用其他函数时,需要先把main函数的现场保存下来,放一边,即压栈,这时候,被调用的函数即可执行,且执行完成后,可加到调用者main,回到main函数后,main函数可继续向后执行。 2、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
13、生成器、协程
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)
14、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]
15、利用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)
本文出自 “11727697” 博客,请务必保留此出处http://11737697.blog.51cto.com/11727697/1792744
原文地址:http://11737697.blog.51cto.com/11727697/1792744