复习:
文件操作 open(文件路径, mode="", encoding="") mode: r 只读 w 只写 a 追加写 r+ 读写 w+ 写读 a+ 追加写读 b 处理分文本文件 光标: seek(0) seek(0,2) tell() truncate() 截断 r+ 深坑, 不论你读取多少内容.写入的时候写在最后.
文件修改 import os with open("修改的目标文件", mode="r", encoding="utf-8") as f1,\ open("修改的目标文件_副本", mode="w", encoding="utf-8") as f2: for line in f1: """修改操作""" s= line.(方法:replace等) f2.write(s) #删除原来文件 os.remove("修改的目标文件") os.rename("修改的目标文件_副本", "修改的目标文件")
习题
ls = [1,2,3,4,5,6,7]print(ls[1:3])ls[1:3] = "abcd"print(ls)ls = [1,"a","b",3,4,5,6,7]print(ls)a = ("hello")print(type(a)) #a = (23)print(type(a)) # a =(23,)print(type(a)) # ---------------------------------------#可以表示FALSE的数据ls = ["",0,list(),set(),tuple(),None]--------------------------------------s1 = "大哥大"s2 = s1.encode("utf-8")print(s2)s3 = s2.decode("utf-8").encode("gbk")print(s3)--------------------------------------------ls = "1,2,3".split(",")print(ls)s = ",".join(ls)print(s)--------------------------------------------ls = ["a","b","c","d"]ls.insert(1,"hello")print(ls) #["a", "hello", "b", "c", "d"]-----------------------------------------------#range打印100,99.....print(list(range(100,-1,-1)))-----------------------------------------------sum = 0i = 1fuhao = 1for i in range(1,100,2): sum = sum + i * fuhao fuhao = fuhao * -1print(sum)------------------------------------------------sum = 0count = 1for i in range(1,100,2): if count % 2 == 0: sum = sum - i else: sum = sum + i count += 1print(sum)----------------------------------------------sum = 0index = 1fuhao = 1while index < 100: sum = sum + index * fuhao index = index + 2 fuhao = fuhao * -1-----------------------------------------------s = "js:剑圣|jj:剑姬|gg:吉吉"ls1 = s.split("|")print(ls1) #["js:剑圣", "jj:剑姬", "gg:吉吉"]d = {}for el in ls1: #在for循环中更改 lst = el.split(":") key = lst[0] value = lst[1] print(key,value) d[key] = valueprint(d)-------------------------------------------------------#整数叠加器content= input("请输入运算")content.replace(" ","") #去掉空格ls = content.split("+")sum = 0for el in ls: print(el) sum = sum + int(el)print(sum)--------------------------------------------------------ls = []lst= ["波多野结衣","×××","苍井空","小泽玛利亚","janson"]index = 0while index < 3: content = input("shuru") for el in lst: if el in content: content = content.replace(el,"*"*len(el)) #不赋值不能改变字符串 ls.append(content) index += 1print(ls)----------------------------------------------------------a = "发大发发发地方撒"print(id(a))a = a.replace("发","哈") #重新指向新的内存print(id(a))print(a)----------------------------------------------------------ls = []for i in range(10): num = int(input("输入数字")) ls.append(num)key1 = []key2 = []for el in ls: if el > 55: key1.append(el) else: key2.append(el)dic = {"key1":key1,"key2":key2}print(dic)----------------------------------------------------f = open("11",mode = "r",encoding="utf-8")# f.readline()若果有表头可以用来出去表头s = []for line in f: ls = line.strip().split(" ") d = {} for el in ls: l2 = el.split(":") d[l2[0]] = l2[1] s.append(d)print(s)
函数:
函数 函数是对功能的封装 语法: def 函数名(形参列表): 函数体(代码块, return) 调用: 函数名(实参列表)
返回值 return : 在函数执行的时候. 如果遇到return. 直接返回 1.如果函数什么都不写, 不写return, 没有返回值. 得到的是None 2.在函数中间或者末尾写return, 返回的是None 3.在函数中写return 值. 返回一个值. 4.在函数中可以返回多个返回值, return 值1, 值2, 值3…., 接收的是元组
参数 函数执行的时候给函数传递信息. *形参:函数声明的位置的变量 *实参:函数调用的时候给的具体的值 传参:把实参交给形参的过程 1. 实参: 1. 位置参数, 按照形参的参数位置, 给形参传值 2. 关键字参数, 按照形参的名字给形参传值 3. 混合参数. 即用位置参数, 也用关键参数 2. 形参: 1. 位置参数 2. 默认值参数 先位置后默认值 3. 动态参数(预习预习)
def opensoftware(tools): #形参 变量 print("打开%s中,请稍等。。。"%tools) print("软件升级中") returnopensoftware("微信") #实参def chi(good_food,no_good_food,drink,ice_cream): print(good_food,no_good_food,drink,ice_cream) return Nonechi("红烧肉","鸡蛋炒饭","老白干","哈根达斯")chi(good_food="蛋炒饭",no_good_food="咸菜",ice_cream="哈根达斯",drink="哈尔滨啤酒")chi("蛋炒饭","咸菜",ice_cream="哈根达斯",drink="哈尔滨啤酒") #位置参数和实参不能交叉def register(name,num,sex): return name,num,sexco = register("the shy","10086","male")print(co) #("the shy", "10086", "male"),说明函数返回多个参数是以元组形式#默认参数#默认参数def register(name,num,gender = "male"): #默认值参数必须在位置参数后面 return name,num,genderco = register("Roocke","10010")print(co) #("Roocke", "10010", "male")
品略图书馆 http://www.pinlue.com/