课程大纲:
1、编码
2、集合
3、深浅拷贝
4、文件操作
5、初识函数
1.编码:
# b1 = s1.encode('utf-8') # print(b1) b1 = b'alex' print(s1.capitalize()) print(b1.capitalize()) s1 = '中国' b1 = s1.encode('utf-8') print(b1) s1 = 'alex' # str ---> bytes encode 编码 b1 = s1.encode('utf-8') print(b1) #bytes---> str decode 解码 s2 = b1.decode('utf-8') print(s2) s1 = 'alex' b2 = s1.encode('gbk') s3 = b2.decode('gbk') print(b2) print(s3) s1 = 'alex' b1 = s1.encode('utf-8') s2 = b1.decode('gbk') print(s2) s4 = '中国' b4 = s4.encode('utf-8') print(b4) s5 = b4.decode('gbk') print(s5) s4 = '中国' b4 = s4.encode('utf-8') # utf-8 bytes print(b4) b6 = b4.decode('utf-8').encode('gbk') print(b6)
2.结合:
set1 = {'alex', 'wusir', 'taibai', 'alex'} set2 = {'alex', 'wusir', 'taibai', 'alex',[1, 2, 3]} print(set1) print(set2) 列表的去重 set1 = {1,1,2,2,3,4,4,5} l1 = [1, 1, 2, 2, 3, 4, 4, 5] print(list(set(l1))) set1 = {'alex', 'wusir', 'ritian', 'egon', 'barry'} 增 set1.add('文周') print(set1) set1.update('abc') print(set1) 删 set1.pop() #随机删除 print(set1) set1.remove('alex') # 按照元素删除 set1.remove('alex1') # 按照元素删除 print(set1) set1.clear() #清空 print(set1) # set() del set1 print(set1) 查 for i in set1: print(i) set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} 交集 & intersection print(set1 & set2) print(set1.intersection(set2)) 并集 | union print(set1 | set2) print(set1.union(set2)) 差集 - difference print(set1 - set2) print(set1.difference(set2)) print(set2 - set1) 反交集 ^ symmetric_difference print(set1 ^ set2) print(set1.symmetric_difference(set2)) 子集 set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5, 6} print(set1 < set2) print(set1.issubset(set2)) # 超集 # set1 = {1, 2, 3} # set2 = {1, 2, 3, 4, 5, 6} # print(set2 > set1) # print(set2.issuperset(set1)) # set1 = {1, 2, 3} # print(frozenset(set1))
3.深浅拷贝
s1 = [1, 2, 3] s2 = s1 # 共用一个 s1.append(666) print(s1, s2) s1 = [1, 2, 3] s2 = s1.copy() s1.append(666) print(s1, s2) s1 = [1, 2, 3,[11,22]] s2 = s1.copy() s1[-1].append(666) print(s1, s2) print(id(s1), id(s2)) print(id(s1[-1]), id(s2[-1])) # 浅copy 第一层各自独立,从第二层开始,共用一个内存地址。 # 深copy import copy s1 = [1, 2, 3,[11,22]] s2 = copy.deepcopy(s1) s1.append(666) print(s1, s2) s1 = [1, 2, 3,[11,22]] s2 = copy.deepcopy(s1) s1[-1].append(666) print(s1, s2) 深copy 无论多少层,都是互相独立的。 切片 浅copy s1 = [1, 2, 3, [11, 22]] s2 = s1[:] # s1.append(666) s1[-1].append(666) print(s1, s2)
4.文件操作
f1 = open(r'd:/嫂子护士联系方式.txt', encoding='gbk', mode='r') print(f1.read()) f1.close() f1 = open('log1', encoding='gbk', mode='r') print(f1.read()) f1.close() """ f1 文件句柄,f,file,file_hander,f_h.... open()调用的内置函数,内置函数调用的系统内部的open, 一切对文件进行的操作都是基于文件句柄f1. 执行流程: 1,打开文件,产生文件句柄。 2,对文件句柄进行操作。 3,关闭文件句柄。 """ 读 r 1 read() 全读出来 f1 = open('log1', encoding='utf-8') content = f1.read() print(content) f1.close() 2 read(n) 读一部分 f1 = open('log1', encoding='utf-8') content = f1.read(3) print(content) f1.close() f1 = open('log1', mode='rb') print(f1.read(3).decode('utf-8')) f1.close() r 模式 read(n) n 按照字符读取。 rb 模式 read(n) n 按照字节读取。 #3 readline() 按行读取 f1 = open('log1', encoding='utf-8') print(f1.readline()) print(f1.readline()) print(f1.readline()) f1.close() 4 readlines() f1 = open('log1', encoding='utf-8') print(f1.readlines()) f1.close() 5 for 循环 f1 = open('log1', encoding='utf-8') for line in f1: print(line) f1.close() f1 = open('1.jpg', mode='rb') print(f1.read()) f1.close() r+ 读写 先读后写 f1 = open('log1', encoding='utf-8', mode='r+') # print(f1.read()) # f1.write('666') f1.write('a') print(f1.read()) f1.close() 写:w w 没有文件,新建文件写入内容 有原文件,先清空内容,在写入新内容。 f1 = open('log2', encoding='utf-8', mode='w') f1.write('桃白白fdksagdfsa') f1.close() 图片的读取及写入 f1 = open('1.jpg', mode='rb') content = f1.read() f2 = open('2.jpg', mode='wb') f2.write(content) f1.close() f2.close() w+ 先写后读 f1 = open('log2', encoding='utf-8', mode='w+') f1.write('两款发动机了') f1.seek(0) print(f1.read()) f1.close() 追加 a a 没有文件,新建文件写入内容 f1 = open('log3', encoding='utf-8', mode='a') # f1.write('alex 666') f1.write('\nalex 666') f1.close() a+ f1 = open('log3', encoding='utf-8', mode='a+') f1.write('python22期') f1.seek(0) print(f1.read()) f1.close() 其他操作方法 readable 是否可读 writable 是否可写 f1.seek(12) # 任意调整 f1.seek(0,2) #光标调整到最后 f1.seek(0) #光标调整到开头 f1.tell() # 告诉光标的位置
5.初始函数
s = 'lkfjsjulkjdgjdsf' count = 0 for i in s: count += 1 print(count) l1 = [1, 2, 3, 4, 5, 6] count = 0 for i in l1: count += 1 print(count) 重复代码多。 可读性差。 s = 'lkfjsjulkjdgjdsf' print(len(s)) s = 'lkfjsjulkjdgjdsf' def my_len(): count = 0 for i in s: count += 1 # print(count) ''' def 关键字 函数名(): 函数体 函数执行:函数名() 函数:以功能为导向。 ''' # my_len() s = 'lkfjsjulkjdgjdsf' def my_len(): count = 0 for i in s: count += 1 print(my_len()) return 1,终止函数。 2,给函数的执行者返回值。 """ return 或者 return None return 单个值 return 多个值 会将多个值放到一个元组中,将元组返回个函数的执行者 """ def func1(): print(111) print(222) return print(333) func1() def func1(): print(111) print(222) return 666 return 'alex' return 'alex', 666, [1, 2, 3] ret = func1() print(ret,type(ret)) s = 'lkfjsjulkjdgjdsf' def my_len(): count = 0 for i in s: count += 1 return count print(my_len()) 函数的传参 def my_len(argv): # 形式参数 ,形参 count = 0 for i in argv: count += 1 return count s = 'lkfjsjulkjdgjdsf' l1 = [1, 2, 3, 4, 5] # my_len(s) # 实际参数, 实参 print(my_len(l1)) 实参角度 位置参数 按照顺序一一对应 def func1(a, b, c): print(a, b, c) func1(1, 2, 'alex') def max(a, b): return a if a > b else b ret = 1 if 2 > 1 else 6 print(ret) print(max(10, 2)) 关键字传参 一一对应。 def func2(a, b): print(a, b) func2(b=2, a=3) 混合参数。(位置参数,关键字参数) 关键字参数必须在位置参数后面。 def func3(a, b, c, d): print(a, b, c, d) func3(1,2,d=3,c=5) 形参角度 位置参数。按顺序一一对应。 def func1(a, b, c): print(a, b, c) func1(1, 2, 'alex') 默认参数。 默认参数在位置参数的后面。 def func1(): print(a, b, c) func1(1, 2, 'alex') def login(name,sex='男'): with open('register', encoding='utf-8', mode='a') as f1: f1.write('{},{}\n'.format(name,sex)) while True: name = input('请输入姓名:').strip() if '1' in name: login(name) else: sex = input('请输入性别:').strip() login(name,sex) 动态参数。 *args, **kwargs 万能参数。 args:所有的位置参数,放在一个元组中。 kwargs:所有的关键字参数,放在一个字典中。 def func3(*args, **kwargs): #函数的定义的时候 * 代表聚合。 print(args) print(kwargs) # func3(1, 2, 3, 'alex', c=6, name='wusir', age='21') # func3(*[1, 2, 3],*(22, 33)) #函数的执行的时候 * 代表打散。 func3(**{'name':"alex"},**{'age':23}) #函数的执行的时候 * 代表打散。 func3(1, 2, 3, 22, 33) #函数的执行的时候 * 代表打散。 形参的顺序 位置参数 *args, 默认参数,**kwargs def func5(a,b,*args,sex='男',**kwargs): print(a,b,) print(args) print(sex) print(kwargs)