作业:
#1.处理文件,用户指定要查找的文件内容,将文件包含要查找内容的每一行都输出到屏幕 # def check_file(filename,aim): # with open(filename,encoding="utf-8") as f: # for line in f: # if aim in line: # yield line # g=check_file('生成器函数进阶.py','send') # for i in g: # print(i.strip()) #2.写生成器,从文件中读取内容,在每一次读取到的内容前加上'***'之后返回给用户 def check_file(filename): with open(filename,encoding="utf-8") as f: for line in f: yield '***'+line for i in check_file('生成器函数进阶.py'): print(i.strip())
内置函数:
# print(callable(print)) #显示是否可用 # import time # for i in range(1000): # time.sleep(1) # print("\r%s"%i,end='') #print打印最后自动有个回车 用end可以去掉 # print(1,2,3,sep="|") #指定输出多个值之间的分隔符 #打印进度条 import time for i in range(0,101,2): time.sleep(0.1) num_p=i//2 print("\r{}%:{}".format(i,"*"*num_p),end="") # exec() #带流程的 # eval() #运行里面的运算 ret=compile('1+2',"",'eval') #编译成exec 或者 eval 模式 , print(eval(ret)) # abs() 取绝对值 # divmod() 除余 # print(divmod(7,2)) # round() 小数值精确 # pow() 求幂运算 第三个参数取余 print(min(1,2,3,-4,key=abs))