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

    python中的68个内置函数

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

    一,作用域相关

    1 locals

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

    2 globals

    获取全局的变量

    二  str类型代码的执行3

    详细的博客http://www.cnblogs.com/wanghaohao/diary/2017/08/01/7269282.html 

    eval, evec, compile

    三   和数字相关

    1 数字类型4bool,int,float,complex

    2 进制转换3 bin,oce,hex

    3数学运算7 min,max,round,pow,sum,abs,divmod

    四 和数据结构有关

    序列---列表和元祖list和tuple

    序列---字符串相关format,str,bytes,bytesarry,memoryview,ord,chr,ascii,repr

    序列---相关内置函数reserved和slice

    数据集合---字典和集合dict,set,frozenset

    数据集合:len,sorted,enumerate,all,any,zip,filter,map

     filter和map:http://www.cnblogs.com/wanghaohao/diary/2017/08/01/7269433.html 

    sorted方法:http://www.cnblogs.com/wanghaohao/diary/2017/08/01/7269461.html 

    其他

    input输入

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

    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: 立即把内容输出到流文件,不作缓存
        """
    View Code

    file关键字的说明

    f = open('tmp_file','w')
    print(123,456,sep=',',file = f,flush=True)
    View Code

    实现进度条

    import time
    import sys
    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='', file=sys.stdout, flush=True)
    View Code

    数据类型相关:

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

    内存相关:

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

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

    t = (1,2,3)
    l = [1,2,3]
    print(hash(t))  #可hash
    print(hash(l))  #会报错
    
    '''
    结果:
    TypeError: unhashable type: 'list'
    '''
    View Code

    hash函数会根据一个内部的算法对当前可hash变量进行处理,返回一个int数字。

    *每一次执行程序,内容相同的变量hash值在这一次执行过程中不会发生改变。

    文件操作相关

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

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

    可以用encoding指定编码.

    模块操作相关

    __import__导入一个模块

    import time
    View Code

    帮助方法

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

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

    和调用相关

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

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

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

    查看参数所属类型的所有内置方法

    dir() 默认查看全局空间内的属性,也接受一个参数,查看这个参数内的方法或变量

    print(dir(list))  #查看列表的内置方法
    print(dir(int))  #查看整数的内置方法
    View Code
  • 相关阅读:
    4.12作业
    4.9上机作业
    第十周上级作业
    第九周上机作业
    第八周作业
    第八周上机作业
    第七周作业
    第七周上机练习
    第六周作业
    4.9上机练习
  • 原文地址:https://www.cnblogs.com/wanghaohao/p/7269265.html
Copyright © 2011-2022 走看看