2018年5月13日

内置模块

摘要: (1)collections模块 (翻译:收藏)在内置数据类型(dict,list,set,tuple)的基础上,collections模块还提供了几个额外的数据类型:counter,deque,defaultdict,namedtuple和OrderedDict等。 1. namedtuple:生 阅读全文

posted @ 2018-05-13 16:14 python21-李伟 阅读(90) 评论(0) 推荐(0) 编辑

2018年5月11日

递归

摘要: 在函数的内部调用自己递归的最大深度:998例子:猜年龄def age(n): #n = 1 ,n = 2 if n == 3: #n = 3 return 40 #返回40 else: return age(n+1)+2 #n = 2 ,n = 3print(age(1))# 递归求解二分查找算法: 阅读全文

posted @ 2018-05-11 23:59 python21-李伟 阅读(95) 评论(0) 推荐(0) 编辑

2018年5月4日

内置函数

摘要: ret = zip([1,2,3,4,5],('a','b','c','d'),(4,5)) #拉链方法print(ret)for i in ret: print(i)lst = [1, 4, 6, 7, 9, 12, 17]def func(num): if num % 2 == 0:return 阅读全文

posted @ 2018-05-04 23:01 python21-李伟 阅读(81) 评论(0) 推荐(0) 编辑

2018年5月1日

列表推导式

摘要: #麻烦办法new_lst = []for i in range(10): new_lst.append(i**2)print(new_lst)#简单办法print([i**2 for i in range(10)])# 小题下面列表中取余list_a = [1,2,3,-5,20,-7]print( 阅读全文

posted @ 2018-05-01 19:44 python21-李伟 阅读(125) 评论(0) 推荐(0) 编辑

生成器

摘要: 判断是否可迭代和是否迭代器from collections import Iterable,Iteratorprint(range(10000))print(isinstance(range(10000),Iterable)) #是否可迭代print(isinstance(range(10000), 阅读全文

posted @ 2018-05-01 16:59 python21-李伟 阅读(83) 评论(0) 推荐(0) 编辑

2018年4月21日

闭包

摘要: 内层函数对外层函数非全局变量的引用,叫做闭包。 闭包的好处:如果python 检测到闭包,有一个机制,你的局部作用域不会随着函数的结束而结束。 def weapper(): name1='老男孩' # def inner(): #中间部分叫闭包 print(name1) inner() # wrap 阅读全文

posted @ 2018-04-21 21:52 python21-李伟 阅读(102) 评论(0) 推荐(0) 编辑

函数的进阶

摘要: 临时名称空间 name = 'alex' age = 12 def funcl(): name1 = 'wusir' age1 = 34 funcl() #临时名称空间:临时名称空间,局部名称空间,存入函数里面的变量与值的关系,随着函数的执行结束,临时名称空间结束。 名称空间:全局名称空间,局部名称 阅读全文

posted @ 2018-04-21 21:48 python21-李伟 阅读(135) 评论(0) 推荐(0) 编辑

函数名,函数体,返回值,参数

摘要: 1. 函数 def 函数名(): #函数名与变量相同 函数体 (1) 函数返回值: return 1. 遇到return,结束函数 def list_a(): print(123) print(456) return print(798) print(abc) list_a() #输出结果 123  阅读全文

posted @ 2018-04-21 00:37 python21-李伟 阅读(403) 评论(0) 推荐(0) 编辑

2018年4月19日

python 文件处理

摘要: 变量 = open(r'文件路径)','r[打开文件模式]',encoding='utf-8文本字符集[默认是unicode]')win 需要在路径上加 r ,字符集跟文件写入的格式. (1). 打开文件的模式有(默认为文本模式[test]):r,只读模式【默认模式,文件必须存在,不存在则抛出异常】 阅读全文

posted @ 2018-04-19 21:35 python21-李伟 阅读(138) 评论(0) 推荐(0) 编辑

2018年4月12日

文件操作练习题

摘要: ''' 1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数。 apple 10 3 tesla 100000 1 mac 3000 2 lenovo 30000 3 chicken 10 3 通过代码,将其构建成这种数据类型: [{'name':'apple','price':10,'a 阅读全文

posted @ 2018-04-12 16:04 python21-李伟 阅读(210) 评论(0) 推荐(0) 编辑

导航