摘要: ''' 高阶函数: 1 把一个函数名当作实参传递给另外一个函数(在不修改被装饰函数源代码情况下,为其添加功能) 2 返回值中包含函数名 ''' import time #为bar函数添加计时功能 # def foo(func): # start_time = time.time() # func() # finish_time = time.time() #... 阅读全文
posted @ 2018-03-25 10:04 与君同悦 阅读(113) 评论(0) 推荐(0)
摘要: ''' 递归: 1 必须有一个明确的结束条件 2 每次进入更深一层的递归时,问题的规模应该有所减少 3 递归效率不高,递归层数过多会导致栈溢出 ''' #示例 def calc(n): m = n/2 print(m) if int(m) == 0: return m return calc(m) a = calc(100) ... 阅读全文
posted @ 2018-03-24 14:57 与君同悦 阅读(92) 评论(0) 推荐(0)
摘要: ''' 1 位置参数不能在关键字参数之后 2 *args只能用于位置参数 把位置参数打包成元组 3 **kwargs只能用于关键字参数 把关键字参数打包成字典 ''' import time def logger(): time_format = "%Y-%m-%d %X" current_timestamp = time.strftime(time_format) ... 阅读全文
posted @ 2018-03-23 16:54 与君同悦 阅读(96) 评论(0) 推荐(0)
摘要: ''' 文件操作 ''' #文件只读模式 r只读 # f = open('hello2.txt', 'r', encoding="UTF-8") # data1 = f.read(10) # print(data1) # #移动指针到某个位置 # f.seek(0) # #打印指针位置 # print(f.tell()) # data = f.read() # f.readline() ... 阅读全文
posted @ 2018-03-19 08:41 与君同悦 阅读(114) 评论(0) 推荐(0)
摘要: a = set([1,2,3,4]) b = set([3,4,5,6]) c = set([1,2]) print("a集合:", a) print("b集合:", b) print("c集合:", c) #增 # a.add('c') #删 # a.clear() # a.pop() #随机删除 # a.remove(1) #删除指定元素,删除不存在的会报错 # print(a.... 阅读全文
posted @ 2018-03-16 16:34 与君同悦 阅读(166) 评论(0) 推荐(0)
摘要: dict = { 'name': 'Eric', 'age': 23, 'height': 170,} for k in dict.keys(): print(k) for v in dict.values(): print(v) for k, v in dict.items(): print(k, 阅读全文
posted @ 2018-03-12 11:30 与君同悦 阅读(183) 评论(0) 推荐(0)
摘要: #列表常用操作 import copy family = ['xiaochun', 'xiaomei', ['xiaohang', 'xiaofang', 'xiaoyue']] #增 family.insert(1,'xiaoshan') print("Add BaiShanshan to index 1:\n", family) family.append('xiaoqian') pr... 阅读全文
posted @ 2018-03-12 09:38 与君同悦 阅读(128) 评论(0) 推荐(0)
摘要: 1 ''' 2 字符串常用的方法 3 ''' 4 5 str = "my name is {name}.I'm {age}" 6 num = "222SSS" 7 alp = "adasd" 8 9 #字符串通过特定字符拆分成列表 10 new_str = str.split(' ') 11 print(new_str) 12 13 #列表用指定... 阅读全文
posted @ 2018-03-12 09:23 与君同悦 阅读(144) 评论(0) 推荐(0)