python笔记15—day15

1、面试题

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(g1))
print(list(g2))

'''
结果为:
[0, 1, 2, 3]
[]
'''
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=(add(n,i) for i in (add(n,i) for i in (add(n,i) for i in test())))
    # g=(15,16,17,18)
print(list(g))

2、内置函数

print(locals())  #返回本地作用域中的所有名字
print(globals()) #返回全局作用域中的所有名字
#迭代器.__next__()
# next(迭代器)
# 迭代器 = iter(可迭代的)
# 迭代器 = 可迭代的.__iter__()

g=range(10).__iter__()
g1=iter(range(10))
print(next(g))
print(next(g))
print(g.__next__())
print(g1.__next__())
# dir 查看一个变量拥有的方法
print(dir([]))
print(dir(1))

# help 跟dir一样
help(str)
# 变量 判断是否是函数
print(callable(print))
a = 1
print(callable(a))
print(callable(globals))
def func():pass
print(callable(func))
import time
print(time.time())

t = __import__('time')#调用time模块,相当于import time
print(t.time())
f = open('001.py')
print(f.writable())#判断文件能不能读
print(f.readable())#判断文件能不能写
#hash - 对于相同可hash数据的hash值在一次程序的执行过程中总是不变的
#     - 字典的寻址方式

print(hash(12345))
print(hash('hsgda不想你走,nklgkds'))
print(hash('hsgda不想你走,nklgkds'))
print(hash(('1','aaa')))
#把需要输出的内容,写到文件里
f = open('file','w')
print('aaaa',file=f)
f.close()
print('我们的祖国是花园',end='')  #指定输出的结束符
print('我们的祖国是花园',end='\n')
print(1,2,3,4,5,sep='|') #指定输出多个值之间的分隔符
#列子:打印进度条
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)
exec('print(123)')
eval('print(123)')
print(eval('1+2+3+4'))   # 有返回值
print(exec('1+2+3+4'))   #没有返回值
# exec和eval都可以执行 字符串类型的代码
# eval有返回值  —— 有结果的简单计算
# exec没有返回值   —— 简单流程控制
# eval只能用在你明确知道你要执行的代码是什么
#compile用法
code = '''for i in range(10): print(i*'*') ''' exec(code) code1 = 'for i in range(0,10): print (i)' compile1 = compile(code1,'','exec') exec(compile1) code2 = '1 + 2 + 3 + 4' compile2 = compile(code2,'','eval') print(eval(compile2)) code3 = 'name = input("please input your name:")' compile3 = compile(code3,'','single') exec(compile3) #执行时显示交互命令,提示输入 print(name)
print(bin(10))#转为二进制
print(oct(10))#转为八进制
print(hex(10))#转为十六进制
print(abs(-5))#绝对值
print(abs(5))
print(divmod(7,2))   # div除法 mod取余
print(divmod(9,5))   # 除余
print(round(3.14159,3))#规定取多少位小数
print(pow(2,3))   #pow幂运算  == 2**3
print(pow(3,2))
print(pow(2,3,3)) #幂运算之后再取余
print(pow(3,2,1))
ret = sum([1,2,3,4,5,6])
print(ret)

ret = sum([1,2,3,4,5,6],10)
print(ret)
print(min([1,2,3,4]))
print(min(1,2,3,4))
print(min(1,2,3,-4))
print(min(1,2,3,-4,key = abs))
print(max([1,2,3,4]))
print(max(1,2,3,4))
print(max(1,2,3,-4))
print(max(1,2,3,-4,key = abs))

猜你喜欢

转载自www.cnblogs.com/xiao-le/p/11522684.html