Python day15

1.复习

2.生成器面试题

例子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]
[]

例子2:

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))

执行结果:

[0, 1, 2, 3]
[]
[]

 例子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)
print(list(g))

执行结果:

[15, 16, 17, 18]

2.内置函数

http://www.cnblogs.com/Eva-J/articles/7206498.html#compile

接下来,我们就一起来看看python里的内置函数。截止到python版本3.6.2,现在python一共为我们提供了68个内置函数。它们就是python提供给你直接可以拿来使用的所有函数。这些函数有些我们已经用过了,有些我们还没用到过,还有一些是被封印了,必须等我们学了新知识才能解开封印的。那今天我们就一起来认识一下python的内置函数。这么多函数,我们该从何学起呢?

 

  Built-in Functions  
abs()dict()help()min()setattr()
all()dir()hex()next()slice()
any()divmod()id()object()sorted()
ascii()enumerate()input()oct()staticmethod()
bin()eval()int()open()str()
bool()exec()isinstance()ord()sum()
bytearray()filter()issubclass()pow()super()
bytes()float()iter()print()tuple()
callable()format()len()property()type()
chr()frozenset()list()range()vars()
classmethod()getattr()locals()repr()zip()
compile()globals()map()reversed()__import__()
complex()hasattr()max()round() 
delattr()hash()memoryview()set() 

上面就是内置函数的表,68个函数都在这儿了。这个表的顺序是按照首字母的排列顺序来的,你会发现都混乱的堆在一起。比如,oct和bin和hex都是做进制换算的,但是却被写在了三个地方。。。这样非常不利于大家归纳和学习。那我把这些函数分成了6大类。你看下面这张图,你猜咱们今天会学哪几大类呀? 

如图:

https://www.processon.com/view/link/597fcacfe4b08ea3e2454ece?pw=G76Z

那要学的一共4块,咱们从哪儿开始学起呢?

作用域相关

 

基于字典的形式获取局部变量和全局变量:

globals()——获取全局变量的字典

locals()——获取执行本方法所在命名空间内的局部变量的字典

迭代器/生成器相关

 

range是可迭代的不是迭代器。

 

其他

 

callable检测是不是一个函数

print(callable(print))
a = 1
print(callable(a))
print(callable(globals))
def func():pass
print(callable(func))

help,查询的str所有的方法

help(str)

import

import time
# t = __import__('time')
# print(t.time())

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

file相关的操作,Open

f = open('1.复习.py')
print(f.writable())
print(f.readable())

id 内存地址,

hash

#hash - 对于相同可hash数据的hash值在一次程序的执行过程中总是不变的
#     - 字典的寻址方式
# print(hash(12345))
# print(hash('hsgda不想你走,nklgkds'))
# print(hash(('1','aaa')))
# print(hash([]))#不可哈希的

input()

print()

print('我们的祖国是花园',end='')  #指定输出的结束符
print('我们的祖国是花园',end='')
print(1,2,3,4,5,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) \  # \n是为了在for循环之后加print可以换行打印
         if i == 100 else '\r%s%% : %s' % (i,'*'*char_num)
     print(per_str,end='', flush=True)

princt输出:

def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
    """
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    file:  默认是输出到屏幕,如果设置为文件句柄,输出到文件
    sep:   打印多个值之间的分隔符,默认为空格
    end:   每一次打印的结尾,默认为换行符
    flush: 立即把内容输出到流文件,不作缓存
    """

print源码剖析
f = open('tmp_file','w')
print(123,456,sep=',',file = f,flush=True)
file关键字说明
exec('print(123)')
eval('print(123)')
print(eval('1+2+3+4'))   # 有返回值
print(exec('1+2+3+4'))   #没有返回值
# exec和eval都可以执行 字符串类型的代码
# eval有返回值  —— 有结果的简单计算   
# exec没有返回值   —— 简单流程控制
# eval只能用在你明确知道你要执行的代码是什么,且用eval比较简单,不能为了简单不能忽略了安全,一般不要用。

compile  将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值。

参数说明:   

1. 参数source:字符串或者AST(Abstract Syntax Trees)对象。即需要动态执行的代码段。  

2. 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。当传入了source参数时,filename参数传入空字符即可。  

3. 参数model:指定编译代码的种类,可以指定为 ‘exec’,’eval’,’single’。当source中包含流程语句时,model应指定为‘exec’;当source中只包含一个简单的求值表达式,model应指定为‘eval’;当source中包含了交互式命令语句,model应指定为'single'。

 
  
#流程语句使用exec
code1 = 'for i in range(0,10): print (i)'
compile1 = compile(code1,'','exec')
exec(compile1)
 
  
# 简单求值表达式用eval
code2 = '1 + 2 + 3 + 4'
compile2 = compile(code2,'','eval')
print(eval(compile2))
 
  
#交互语句用single
code3 = 'name = input("please input your name:")'
compile3 = compile(code3,'','single')
exec(compile3) #执行时显示交互命令,提示输入
以后可以用name变量,比如print(name)可以打印出输入的内容

 和数字相关:

complex:基本上不用

复数———complex

实数———有理数:
     无理数:无限不循环实数

虚数:虚无缥缈的数

浮点数(有限循环和无限循环小数) =!小数(有限循环和无限循环小数,无限不循环小数)

print(bin(10))#10进制转换成二进制
print(oct(10))#十进制转换成八进制
print(hex(10))#十进制转换成十六进制

执行结果:

0b1010
0o12
0xa

print(abs(-5))#绝对值
print(abs(5))#绝对值
print(divmod(7,2))   # div除法 mod取余
print(divmod(9,5))   # 除余
#执行结果:

  (3, 1)
  (1, 4)

print(round(3.14159,3))
执行结果,会四舍五入:
3.142
print(pow(2,3))   #pow幂运算  == 2**3
print(pow(3,2))
print(pow(2,3,3)) #幂运算之后再取余
print(pow(3,2,1))
执行结果:
8
9
2
0
ret = sum([1,2,3,4,5,6],10)#start只能以位置传参
print(ret)
执行结果:
31

ret = sum([1,2,3,4,5,6])
print(ret)
执行结果:
21

sum(1,2)
执行结果:3
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))
执行结果:
1
1
-4
1
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))
执行结果:
4
4
3
-4

转载于:https://www.cnblogs.com/liranranwangmeng/p/10081913.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值