集合
查看如下代码,集合的操作基本都在里面了
#Author:Peng Huang set1 = {1,2,3,44,22,45,37} set2 = {1,2,3,4,6,8} set3 = {3,44} print(type(set1)) print(set1.intersection(set2)) #交集 print(set1.difference(set2)) #集合1和集合2的差集 print(set2.difference(set1)) #集合2和集合1的差集 print(set1.union(set2)) #合集 print(set1.issubset(set3)) #判断集合1是不是集合3的子集 print(set3.issubset(set1)) #判断集合3是不是集合1的子集 print(set1.issuperset(set3)) #判断集合1是不是集合3的父集 print(set1.isdisjoint(set2)) #判断集合1和集合2是不是没有合集,如果没合集,返回为真 print(set1.symmetric_difference(set2)) #对称差集,合集去掉交集 #运算符 print(set1 & set2) #交集 print(set1 | set2) #合集 print(set1 - set2) #差集,项在集合1,不在集合2 print(set1 ^ set2) #对称差集 #集合操作 set1.add(72) #添加一项 print(set1) set1.update([11,23.34]) #添加多项 print(set1) set1.remove(72) #删除一项,不存在会报错 print(set1.pop()) #随机删除一项,并返回删除的项 set1.discard(888) #删除一项,不存在也不会报错 print(set1) print(len(set1)) #集合长度 print('x' in set1) #判断是不是在集合里 print('x' not in set1)
文件
文件基本读写
#Author:Peng Huang #写入文件,新创建文件,会覆盖以前的文件内容 ''' file1 = open("file_operation_test.txt","w",encoding="utf-8") file1.write("你知道我是谁吗?\n") file1.write("我是你爸爸!") file1.close() #添加内容 file1 = open("file_operation_test.txt","a",encoding="utf-8") file1.write("\n重要的事情说三遍!\n") file1.write("我是你爸爸!\n") file1.write("我是你爸爸\n我是你爸爸\n") file1.close() ''' #读文件 file1 = open("file_operation_test.txt","r",encoding="utf-8") #f = file1.read() #读一次 #f = file1.readline() #按行读,读一行 #f = file1.readlines() #按行读,读多行,读成列表的形式 #for f in file1.readlines(): #循环读,是一次性读取所有内容,只适合小文件,比较low # print(f.strip()) count = 0 for line in file1: if count == 2: print("------这是分隔符------") count += 1 continue print(line.strip()) count += 1 file1.close()
文件操作及其他模式
#Author:Peng Huang ''' file1 = open("file_operation_test.txt","r",encoding="utf-8") print(file1.name) #文件名 print(file1.encoding) #文件编码 print(file1.tell()) #文件开始指针 print(file1.readline()) #读一行 file1.seek(0) #将指针重新调回开始位置 print(file1.readline()) #在读一行 print(file1.tell()) #打印指针 print(file1.isatty()) #文件是不是终端 file1 = open("file_operation_test.txt","a",encoding="utf-8") file1.truncate(5) #切片 开始五个字符 import sys,time for i in range(50): sys.stdout.write("#") sys.stdout.flush() #flush 将结果刷新,即时写入 time.sleep(0.1) file1 = open("file_operation_test.txt","r+",encoding="utf-8") #读写模式,可读可在后面添加内容 print(file1.readline()) print(file1.readline()) file1.write("\n你猜我是谁\n") print(file1.readline()) file1.close() file1 = open("file_operation_test.txt","w+",encoding="utf-8") #写读模式,新创建一个文件,然后写入内容,然后读取 file1.write("你猜我是谁\n") file1.write("我是你爸爸!\n对,我就是你爸爸!\n你不服也不行,赶紧叫爸爸!\n") file1.seek(0) print(file1.readline()) print(file1.readline()) file1.close() file1 = open("file_operation_test.txt","a+",encoding="utf-8") #追加读写 file1.write("你再次猜我是谁\n") file1.write("我还是你爸爸!\n对,我就是你爸爸!\n你不服也不行,赶紧叫爸爸!\n") file1.seek(0) count = 0 for line in file1: print(line.strip()) count += 1 file1.close() file1 = open("file_operation_test.txt","rb") #读取二进制文件 print(file1.readline()) print(file1.readline()) print(file1.readline()) file1.close() ''' file1 = open("file_operation_test.txt","wb") #写入二进制文件 file1.write("我是你爸爸\n".encode()) file1.close()
课堂作业
文件修改
#Author:Peng Huang f1 = open("文件修改前","a+",encoding="utf-8") #先写入文件内容 f1.write("你知道我在等你吗?\n你如果真的在乎我\n") f1.seek(0) #将指针返回开始,方便下面读 f2 = open("文件修改后","w",encoding="utf-8") for line in f1: if "你如果真的在乎我" in line: #匹配是否有这个字符串 line = line.replace("你如果真的在乎我","你真的在乎我吗?") #如果有修改字符串内容 f2.write(line) f1.close() f2.close()
字符编码与转码
在python2.x 里,默认编码是accii,需要先自己的编码解码成Unicode,然后在将Unicode编码成需要的编码
可以参考如下图
需求:将GBK编码译成UTF-8
1.先将GBK解码成Unicode
2.再将Unicode编码成UTF-8
python3.x 默认编码是Unicode,在pycharm里,可以将文件编码声明为非utf-8的其他编码,但是程序输入的变量或者输入都是Unicode编码的。
#Author:Peng Huang #-*-coding:gbk-*- #将文件的编码声明成gbk import sys print(sys.getdefaultencoding()) #查看python的默认编码,还是utf-8 s = '你好' #此时输入的编码是Unicode print(s) print(s.encode("gbk")) #将输入的字符重新编码为gbk,自动转为bytes类型展示 print(s.encode("utf-8").decode("utf-8")) #以utf-8编码,先转为bytes,然后在转为字符串
函数
#Author:Peng Huang #python中函数的定义方法 def test(x): #def 定义函数的关键字 test 函数名 ():定义形参 "The function definitions" #文档描述,函数功能定义 x +=1 #执行逻辑 return x #返回值 #如下为例子 #函数 def func1(): "this is a testing function1" print("this is a testing function1") return 0 #过程 #过程与函数的区别,过程没有返回值,但是python将过程当成函数处理 def func2(): "this is a testing function2" print("this is a testing function2")
# Author:Peng Huang #函数的一致性,可复用行 import time def logger(): time_format = "%Y-%m-%d %X" time_current = time.strftime(time_format) with open("log.txt", "a+", encoding="utf-8") as f: f.write("%s this is the end\n" %time_current) def test1(): print("this is test1") logger() def test2(): print("this is test2") logger() def test3(): print("this is test3") logger() test1() test2() test3()
#Author:Peng Huang #函数的返回值可以是多种的,但是对于函数来说,他的结果只是一个值,无论你值里面放的是什么 def test1(): print("this is test1") def test2(): print("this is test2") return 0 def test3(): print("this is test3") return 1,"hello",[1,2,3,4],{"name":"huang"} print(test1()) print(test2()) print(test3())
#Author:Peng Huang #函数的参数 ''' def test(x,y): #x,y是形参 print(x) print(y) test(1,2) #1,2是实参,位置参数 test(y=2,x=1) #关键字参数,与位置无关 ''' #默认参数 def test(x,y=2): print(x) print(y) test(1,3) #默认参数调用特点:调用参数的时候,默认参数非必须传递 #用途:可以在安装软件的时候,选择默认安装等,默认参数已经传递过去了
#Author:Peng Huang #参数组 def test(*args): print(args) test(1,2,3,4,5,6,7) #接受N个位置参数,转换成元组 def test1(x,*args): print(x) print(args) test1(1,2,3,4,5,6,7) #接受字典为参数 def test2(**kwargs): print(kwargs) test2(name="huang",age=27,sex="male") #以关键字参数传递字典信息 #组合传参 def test4(name,age=18,*args,**kwargs): print(name) print(age) print(args) print(kwargs) test4("huang",12,1,2,3,4,5,sex="m",hobby="reading")
#Author:Peng Huang name = "ma" #全局变量 def change_name(name): print("before change:",name) name = "huang" #局部变量,这个函数就是这个变量的作用域 print("after change:",name) change_name(name) print(name) #其他的数据类型,如列表,字典,集合,如果修改了,会应用到全局 names = ["huang","ma","zhou"] def change_name(): names[0] = "黄" print(names) change_name() print(names)
递归
#Author:Peng Huang #在函数的内部可以调用其他的函数,如果一个函数在内部调用自己,这个函数就叫递归函数 #递归函数必须要有明确的结束条件 #递归进入深一层递归时,问题规模应该比上次递归有所减少 #递归效率不高,层次过多,会导致内存的栈溢出 def calc(n): print(n) if int(n/2) > 0 : return calc(int(n/2)) calc(10)