zoukankan      html  css  js  c++  java
  • 9.内置函数(不常用)

    exal exce

    str() dir() range() len() print() max() min() open()
    input() type() int() enumerate() list() id() set() dict()
    iter() next() tuple() bool() globals() locals() frozenset()
    exal exce
    #慎用
    s1="{1:'alex'}"
    eval 剥去字符串的外衣,返回里面的本质,执行字符串类型的代码,并返回最终结果。
    ret=eval(s1)
    print(ret,type(ret))
    '''
    {1: 'alex'} <class 'dict'>
    '''
    
    s2='1+3'
    ret=eval(s2)
    print(ret,type(ret))
    4 <class 'int'>
    exec 代码流,过程
    s3='''
    for i in range(3):
        print(i)
    '''
    exec(s3)
    '''
    0 
    1
    2
    '''
    s3 = input('>>>')
    print(eval(s3))
    1+3+4
    8
    

    hash():获取一个对象(可哈希对象:int,str,Bool,tuple)的哈希值。

    print(hash(123214134))
    print(hash('fkljdsaklgjdfs'))
    print(hash('gjdfs'))
    123214134
    -992644925656859859
    -755752526777003978
    

    help()函数用于查看函数或模块用途的详细说明。

    print(help(str.count))
    '''
    Welcome to Python 3.6's help utility!
    
    If this is your first time using Python, you should definitely check out
    the tutorial on the Internet at https://docs.python.org/3.6/tutorial/.
    
    Enter the name of any module, keyword, or topic to get help on writing
    Python programs and using Python modules.  To quit this help utility and
    return to the interpreter, just type "quit".
    
    To get a list of available modules, keywords, symbols, or topics, type
    "modules", "keywords", "symbols", or "topics".  Each module also comes
    with a one-line summary of what it does; to list the modules whose name
    or summary contain a given string such as "spam", type "modules spam".
    
    help> 
    '''
    

    callable **函数用于检查一个对象是否是可调用的。如果返回True,object仍然可能调用失败;但如果返回False,调用对象ojbect绝对不会成功。

    def a():
        pass
    
    b = 3433
    
    a()
    
    print(callable(a))
    print(callable(b))
    '''
    True
    False
    '''
    

    int,float,complex

    print(int(3.14))#函数用于将一个字符串或数字转换为整型。
    print(int('123'))
    print(int('0100',base=2))# 将2进制的 0100 转化成十进制。结果为 4
    '''
    3
    123
    '''
    print(float(100))>>>100.0#函数用于将整数和字符串转换成浮点数。
    print(complex(1,2))>>>(1+2j)#返回一个复数
    

    bin oct hex

    print(bin(100))  # 将十进制转化成二进制。 **
    print(oct(10)) # 将十进制转化成八进制字符串并返回。print(hex(17)) # 将十进制转化成十六进制字符串并返回。 **
    print(hex(15))
    '''
    0b1100100
    0o12
    0x11
    0xf
    '''
    
    计算除数与被除数的结果,返回一个包含商和余数的元组(a // b, a % b)分页用到
    print(divmod(10, 3))  # **
    
    保留小数的有效位置
    print(round(3.1485926,2))
    
    print(pow(3, 3))  # 3*3*3
    print(pow(3, 3, 2))
    

    输入字符寻找其在unicode的位置。

    print(ord('a'))
    print(ord('中'))
    '''
    97
    20013
    '''
    

    输入位置数字找出其对应的字符

    print(chr(98))  # **
    print(chr(20104))  # 予
    

    repr 原形毕露 **

    print('太白')
    
    print(repr('太白'))
    
    msg = '我叫%r' %('太白')print(msg)
    太白
    '太白'
    我叫'太白'
    

    all any **

    l1 = [1, 'fgdsa', [], {1: 2}]
    print(any(l1))# 有一个为真,返回真
    print(all(l1))#所有为真,返回真
    '''
    True
    False
    '''
    l2 = [0, '', [], {}]
    print(all(l1)) # 判断可迭代对象元素全部都为True,返回True
    print(any(l2))  # 判断可迭代对象元素只要有一个True返回True
    
  • 相关阅读:
    Java启动工程时,加载固定数据到Map中(不用每次访问数据库)
    Java删除文件夹和其子文件、文件的拷贝和剪切
    EasyExcel导入工具(SpringMVC下使用)
    web工程启动时,在一个类中延迟加载Bean,因为该Bean类可能还没被JVM加载
    ECharts访问后台,JSON格式返回数据实例
    Java 实现缓存,一个线程存,一个线程取
    IE浏览器使用VLC实时显示视频(海康、大华)
    Windows和Linux下 Java开发ping工具类
    Quartz定时器+Spring + @Autowired注入 空指针异常
    ubuntu 安装mysql
  • 原文地址:https://www.cnblogs.com/pythonblogs/p/11089082.html
Copyright © 2011-2022 走看看