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

    1,作用域相关

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

    #返回本地作用域中的所有名字

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

    #返回全局作用域中的所有名字

    2,字符串类型的代码执行

    eval() 将字符串类型的代码执行并返回结果

    print(eval('1+2+3+4')

    exec()将自字符串类型的代码执行

    print(exec("1+2+3+4"))
    exec("print('hello,world')")
    compile编译
    #流程语句使用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) #执行时显示交互命令,提示输入
     

     3、迭代器/生成器相关(3)

    #迭代器.__next__()
    # next(迭代器)
    # 迭代器 = iter(可迭代的)
    # 迭代器 = 可迭代的.__iter__()

    4,输入输出相关
    input() 输入
    s = input("请输入内容 : ")  #输入的内容赋值给s变量
    print(s)  #输入什么打印什么。数据类型是str

     print() 输出源码解析

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

    5,数据类型相关:

    type(a) 返回变量a的数据类型

    6,内存相关:

    id(o) o是参数,返回一个变量的内存地址

    hash(o) o是参数,返回一个可hash变量的哈希值,不可hash的变量被hash之后会报错。

    #hash - 对于相同可hash数据的hash值在一次程序的执行过程中总是不变的
    # - 字典的寻址方式
    t = (1,2,3)
    l = [1,2,3]
    print(hash(t))  #可hash
    print(hash(l))  #会报错
    
    '''
    结果:
    TypeError: unhashable type: 'list'
    '''
    
    hash实例

    7,文件操作相关

    open()  打开一个文件,返回一个文件操作符(文件句柄)

    操作文件的模式有r,w,a,r+,w+,a+ 共6种,每一种方式都可以用二进制的形式操作(rb,wb,ab,rb+,wb+,ab+)

    可以用encoding指定编码.

    8,模块操作相关

    __import__导入一个模块

    9,帮助方法

    help

    在控制台执行help()进入帮助模式。可以随意输入变量或者变量的类型。输入q退出

    或者直接执行help(o),o是参数,查看和变量o有关的操作。。。

    #help与dir的区别:
    dir()
    查看方法名(不是很详细的帮助)
    help
    查看方法名和用法(更详细的帮助)
     
    10,和调用相关

    callable(o),o是参数,看这个变量是不是可调用。

    如果o是一个函数名,就会返回True

     
    def func():pass
    print(callable(func))  #参数是函数名,可调用,返回True
    print(callable(123))   #参数是数字,不可调用,返回False

    11,和数字相关

    数字——数据类型相关:bool,int,float,complex

    数字——进制转换相关:bin,oct,hex

    数字——数学运算:abs,divmod,min,max,sum,round,pow# print(bin(10))--二进制# print(oct(10))--八进制

    # print(hex(10))--十六进制
    #abs()求绝对值
    #divmod() 除余 ->div 除法,mod 取余
    #pow(2,3)->2**3求幂运算
    #pow(2,3,4)-->2*3%4,最后一个数取余。幂运算之后取余
    12,max
    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))
    
    

    13,min

    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))
     
  • 相关阅读:
    ACID
    Elasticsearch SQL
    【协议】AAA Radius协议的常用报文分析
    【linux】内核-模块(驱动)命令原理
    【linux】masm汇编系统调用说明
    pytest 8+.yaml文件详解---实现接口自动化
    谷歌 Chrome 浏览器将迎来设计、媒体播放控件改进
    长期免费的通配符证书获取 2021年最新方法
    Flink:部署运行
    Linux:nc命令
  • 原文地址:https://www.cnblogs.com/kakawith/p/8193930.html
Copyright © 2011-2022 走看看