2018.3.14
#一组例子 def demo (newitem,old=[]): old.append(newitem) return old print(demo('5',[1,2,3,4])) print(demo('aaa',['a','b'])) print(demo('a')) #此处会出现意料之外的情况 print(demo('b')) def demoo(newitem,old=None): if old==None: old=[] old.append(newitem) return old print(demoo('5',[1,2,3,4])) print(demoo('aaa',['a','b'])) print(demoo('a')) print(demoo('b'))
#可变长度参数 def demo(**p): for item in p.items(): print(item) demo(x=1,y=3,z=5) def dem(*p): for item in p: print(item) dem('q',3,5.4) E:\Python\python.exe E:/All_Code/python/T_text/text.py ('x', 1) ('y', 3) ('z', 5) q 3 5.4
f = lambda x,y,z:x+y+z print(f(2,3,4)) print(f('a','r','f')) g = lambda x,y=33,z=1:x+y+z print(g(2,5,5)) print(g(2)) L=[(lambda x:x**2),lambda x:x**3,lambda x:x**4] print(L[0](2),L[2](2))
import random def demo(lst): m = min(lst) result = (m,) for index,value in enumerate(lst): if value == m: result += (index,) return result x = [random.randint(1,20) for i in range(50)] print(x) print(demo(x)) #杨辉三角 def demo1(t): print([1]) print([1,1]) line = [1,1] for i in range(2,t): r=[] for j in range(0,len(line)-1): r.append(line[j]+line[j+1]) line = [1]+r+[1] print(line) demo1(10) E:\Python\python.exe E:/All_Code/python/T_text/text.py [3, 2, 4, 2, 8, 8, 19, 20, 10, 15, 9, 19, 5, 14, 14, 13, 2, 2, 1, 17, 13, 18, 17, 4, 9, 4, 6, 16, 8, 7, 7, 7,
4, 15, 12, 11, 18, 8, 10, 15, 7, 2, 3, 4, 10, 11, 17, 16, 18, 3] (1, 18) [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]