文件检查

def che_file(filename,aim):
	with open(filename,encoding='utf-8') as f:
		for i in f:
			if aim in i:
				yield i
g=che_file('1.txt','python')
for i in g:
	print(i.strip())

每次加/********

def che_file(filename):
	with open(filename,encoding='utf-8') as f:
		for i in f:
			yield '*******'+i
for i in che_file('1.py'):
	print(i)

##面试题

f=(i for i in g)
def func()
for i in g:
yield i

面试大题


 def demo():
    for i in range(4):
         yield i
g=demo()
g1=(i for i in g)
g2=(i for i in g1)
print(list(g))
print(list(g1))
print(list(g2))
def add(n,i):
    return n+i
def test():
    for i in range(4):
        yield i
g=test()
 for n in [1,10,5]:
    g=(add(n,i) for i in g)
n = 1
g=(add(n,i) for i in test())
n = 10
g=(add(n,i) for i in (add(n,i) for i in test()))
n = 5
g=(15,16,17,18)
print(list(g))

今日重点:内置函数

#内置函数 68 个内置函数

作用域:

  • locals()#返回本地作用域中的所有名字
  • globals()#返回全局作用域中的所有名字
    关键字nonlocal global 变量作用域有关

迭代器

range next iter

  • next(迭代器)<==>> next
  • range 只是一个可迭代的 不是迭代器
    print(“next” in dir(iter(range(1,22,33))))

其他 12个

  • dir 查看一个对象拥有的方法
    print(dir(1))

  • callable #查看能否被调用 只对函数
    print(calllable(print))

#变量

  • help #帮助文档 help(str) # 封闭开发

  • import #导入模块 import()

import time 《===》》 time = import(‘time’) #全局空间

#某个方法属于某个数据类型的变量 就用.调用
#如果某个方法 不依赖于任何数据类型 ,就直接调用 ----内置函数 和import

文件句柄

文件 f.writeable()#可不可以写 f.readable() open

内存 id hash()

#对于相同可hash数据的hash值在一次程序的执行过程中总是不变的
key 必须可hash 字典寻址方式

迈向大神的 day13 补发 《函数》

  • 输入 输出
    input()
    print() 自动换行
    print(“women women\n”,end=’’) #默认参数 制定输出的结束符
    print(123,2,3,sep=’|’) #制定输出多个值之前的分隔符

f=open(‘file’,‘w’)
print(‘aaaa’,file=f) 输出到指定文件
f.close()

打印字符条

import time
for i in range(0,101,2):
    time.sleep(0.1)
    char_num = i//2
    per_str = '\r%s%% : %s\n' % (i, '*' * char_num) 
    if i == 100 else '\r%s%% : %s' % (i,'*'*char_num)
             print(per_str,end='', flush=True)

\r 回到行首
progress Bar

字符串 执行

eval exec
eval(“print(123)”) #直接执行

print(1+2+3+4) 10
print(eval(’‘1+2+3+4’)) 10
print(exec(‘1+2+3+4’)) 没有返回值 None
#eval 用在明确知道代码要执行什么 有结果的简单计算
#exec 没有 简单流程控制

code=’’‘for i in range(10):
print(i*’*’)
‘’’
exec(code)
complier()
code=‘for i in range(0,10):print(i)’
compile=compile(code,’’,‘exec’)
exec(compile)

交互式

code3=‘name=input(“please input”)’
com=compile(code3,’’,’ single’)
exec(com)
print(name)

数学

  • complex
    实数–
    复数:虚无缥缈的数
    c.real c+image 实部和虚部 都是float
    5+12j= 实数+虚部===复数
  • 浮点数 3.54*10**2

  • 进制 bin oct hex

  • abs 绝对值

  • divmod(7,5) #除余方法

  • round(3.1415,2) 3.14

  • pow(2,3,3) 2的3次mi 取余 2**3 /3

  • sum([1,23,4,5]) 可迭代的 min 可以是分开的

  • print(min(1,2,3,-4,key=abs))

  • max 同理

易错题

字符串和数字不能相加
3>2==2 true
if (x>y): print(x)
注意看符号

whle i<10:
time.sleep(10) 无限执行下去 因为没有变量条件
global c 全局
如何判断是不是字符串 type(hello)

相关文章:

  • 2021-04-20
  • 2022-01-10
  • 2021-08-27
  • 2021-11-08
  • 2021-08-24
  • 2022-12-23
  • 2021-05-29
  • 2021-09-27
猜你喜欢
  • 2021-11-06
  • 2022-12-23
  • 2021-09-11
  • 2021-11-07
  • 2021-04-26
  • 2021-07-11
  • 2022-12-23
相关资源
相似解决方案