zoukankan      html  css  js  c++  java
  • day15 内置函数

    day15 内置函数
    ★★★★★生成器面试题★★★★★
    def demo():
    for i in range(4):
    yield i

    g = demo()
    g1 = (i for i in g)
    g2 = (i for i in g1) #等同于g2 = (i for i in (i for i in 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]:
    g = (add(n,i) for i in g)
    #以下为拆解详细步骤
    '''n =1
    g =(add(n,i) for i in g)
    g =(add(n,i) for i in (0,1,2,3)) #此时g是(1,2,3,4)
    g = (1,2,3,4)
    n = 10
    g = (add(n,i) for i in (add(n,i) for i in g))
    g = (add(n,i) for i in (add(n,i) for i in (0,1,2,3)))
    g = (add(n,i) for i in (10,11,12,13))
    g = (20,21,22,23)
    print(list((20,21,22,23)))'''
    print(list(g)) #list(g)才开始执行,生成器比较懒,

    print(locals()) #返回本地作用域中的所有名字
    print(globals()) #返回全局作用域中的所有名字
    #global #变量,用于申明全局变量
    #nonlocal #变量,用于函数内部

    range(1,11)
    print('__next__' in dir(range(1,11))) #说明range中没有__next__方法,只是一个可迭代对象,不是一个迭代器

    #dir查看一个变量拥有的方法
    print(dir(int))

    #变量
    print(callable(print)) #查看print是否可以被调用
    a = 1
    print(callable(a))
    print(callable(globals))
    def func():
    pass
    print(callable(func))

    #help
    # help(str)
    #某个方法属于某个数据类型的变量,就用.滴啊用
    #如果某个方法不依赖于任何数据类型,就直接调用

    #ID

    #hash
    print(hash((1,2)))
    print(hash(('1','2')))
    # print(hash([1,2]))
    #对于相同可hash数据的hash值在一次程序的执行过程中总是不变的
    #字典的寻址方式

    #input
    #print
    print("sdgsdgsdgsd",end='') #end执行输出的结束符
    print("sdgsdgsdgsd")
    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 = ' %s%% : %s ' % (i, '*' * char_num) if i == 100 else ' %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只能用在你明确知道你要执行的代码是什么

    code = '''for i in range(10):
    print(i*'*')
    '''
    exec(code)

    #compile 将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值。
    #流程语句使用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')
    eval(compile2)

    #交互语句用single
    code3 = 'name = input("please input your name:")'
    compile3 = compile(code3,'','single')
    #name #执行前name变量不存在
    exec(compile3) #执行时显示交互命令,提示输入

    #复数----complex
    #实数+虚数 == 复数

    #浮点数(有限循环小数 无限循环小数)!=小数: 无限不循环小数,无限循环小数,有限循环小数
    #浮点数
    #354.123 = 3.54123*10**2 = 35.4123*10
    f = 1.784562155774570
    print(f)

    #进制转换
    print(bin(10)) #二进制
    print(oct(10)) #八进制
    print(hex(10)) #十进制
    #输出结果为:
    '''0b1010
    0o12
    0xa'''

    #绝对值abs
    print(abs(-5))

    #divmod除余方法
    print(divmod(7,2))

    #round:四舍五入
    print(round(3.124555654))

    #幂运算
    print(pow(2,3))
    print(pow(3,2,1)) #幂运算之后再取余

    ret = sum([1,2,3,4,5,6],10)
    print(ret)

    #计算最大值最小值
    #max min
    print(min(1,2,3,-4,key=abs))

  • 相关阅读:
    萌新向Python数据分析及数据挖掘 第三章 机器学习常用算法 第三节 梯度下降法 (上)理解篇
    萌新向Python数据分析及数据挖掘 第三章 机器学习常用算法 第二节 线性回归算法 (下)实操篇
    萌新向Python数据分析及数据挖掘 第三章 机器学习常用算法 第二节 线性回归算法 (上)理解篇
    萌新向Python数据分析及数据挖掘 第三章 机器学习常用算法 第一节 KNN算法 (下)实操篇
    萌新向Python数据分析及数据挖掘 第三章 机器学习常用算法 第一节 KNN算法 (上)理解篇
    萌新向Python数据分析及数据挖掘 第二章 pandas 第五节 Getting Started with pandas
    Oracle数据库安装和授权
    c# 如何获取JSON文件以及如何获取Config文件(framework 和 net .Core)
    C#Core查询数据库存储EXCEL文件
    如何在WINDOW系统下编译P12证书制作
  • 原文地址:https://www.cnblogs.com/Murraya/p/10765338.html
Copyright © 2011-2022 走看看