zoukankan      html  css  js  c++  java
  • Python常用标准库函数

      math库:

       

    >>> import math
    >>> dir(math)
    ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
    >>> math.pi
    3.141592653589793
    >>> math.e
    2.718281828459045
    >>> help(math.ceil)
    Help on built-in function ceil in module math:
    
    ceil(...)
        ceil(x)
        
        Return the ceiling of x as an Integral.
        This is the smallest integer >= x.
    
    >>> math.ceil(3.6)
    4
    >>> math.floor(3.6)
    3
    >>> math.pow(2,3)
    8.0
    >>> math.sqrt(4)
    2.0
    >>> math.degrees(3.14)
    179.9087476710785
    >>> math.radians(180)
    3.141592653589793
    

      os库:

       

    import os  
    >>> os.getcwd()  
    'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32'  
    >>> path='e:\test'  
    >>> os.chdir(path)  
    >>> os.getcwd()  
    'e:\test'  
    >>> os.rename('test.txt','test111.txt')  
    >>> os.remove('test111.txt')  
    >>>   
    

      

    random库:

      

    >>> import random  
    >>> random.choice(['C++','Java','Python'])  
    'Java'  
    >>> random.randint(1,100)  
    57  
    >>> random.randrange(0,10,2)  
    8  
    >>> random.random()  
    0.7906454183842933  
    >>> random.uniform(5,10)  
    7.753307224388041  
    >>> random.sample(range(100),10)  
    [91, 15, 67, 38, 55, 72, 62, 97, 51, 77]  
    >>> nums=[1001,1002,1003,1004,1005]  
    >>> random.shuffle(nums)  
    >>> nums  
    [1002, 1004, 1005, 1001, 1003]  
    

      datetime库:

      

    import datetime  
    >>> dir(datetime)  
    ['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'timezone', 'tzinfo']  
    >>> from datetime import date  
    >>> date.today()  
    datetime.date(2018, 3, 7)  
    >>> from datetime import time  
    >>> tm=time(23,20,35)  
    >>> print(tm)  
    23:20:35  
    >>> from datetime import datetime  
    >>> dt=datetime.now()  
    >>> dt  
    datetime.datetime(2018, 3, 7, 15, 1, 7, 13107)  
    >>> print(dt.strftime('%a,%b %d %Y %H:%M'))  
    Wed,Mar 07 2018 15:01  
    >>> dt=datetime(2017,6,6,23,29)  
    >>> print(dt)  
    2017-06-06 23:29:00  
    ts=dt.timestamp()  
    >>> ts  
    1496762940.0  
    print(datetime.fromtimestamp(ts))
    

      

  • 相关阅读:
    python中关于操作时间的方法(一):使用time模块
    size_t类型
    批量下载网络图片并zip打包
    遇到的java面试题
    jsp中button与submit的用法
    springmvc json字符串转化成json对象
    Cas 介绍及使用
    java中post时中文乱码
    mybatis使用generator生成对应的model、mapping配置文件、dao
    移动端接口:java写get方式访问数据(springmvc+spring。。。)
  • 原文地址:https://www.cnblogs.com/68xi/p/8564701.html
Copyright © 2011-2022 走看看