#作业
#杨辉三角形
def triangles():
n=[1]
while True:
yield n
n=[x+y for x,y in zip([0]+n,n+[0])]
n=0
for t in triangles():
print(t)
n=n+1
if n==10:
break
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
list1=[1,2,3]
list2=[4,5,6]
lists=zip(list1,list2)
n=list(lists)
n
[(1, 4), (2, 5), (3, 6)]
[x+y for x,y in zip(list1,list2)]
[5, 7, 9]
[0]+[1,1]
[0, 1, 1]
#使用生成器生成类似特征的字符串 def getUrl(num): ''' 生成url ''' for i in range(num): var=chr(i+97)+str(100*(i+1))+".html" url="www.xdbcb8.com/p/"+var yield url for url in getUrl(10): print(url)
www.xdbcb8.com/p/a100.html
www.xdbcb8.com/p/b200.html
www.xdbcb8.com/p/c300.html
www.xdbcb8.com/p/d400.html
www.xdbcb8.com/p/e500.html
www.xdbcb8.com/p/f600.html
www.xdbcb8.com/p/g700.html
www.xdbcb8.com/p/h800.html
www.xdbcb8.com/p/i900.html
www.xdbcb8.com/p/j1000.html
====================================================
#python的模块和包
#模块就是有你定义的函数,这些函数写在一个*name*.py文件中
在文件中想使用模块中的函数则使用
from *name* import func1
或
import *name*
注意引用的函数不能和标准函数库的名字一样,不然可能会有错误
系统可以从sys.path差找相应的模块
如果想模块导入的时候不执行,可以在模块的.py里加一句
if __name__=="__main__":
%%%%
import sys
sys.path
#python包
每个子文件夹夹中都有__init__.py