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

  

posted @ 2018-03-14 08:53  圆柱模板  阅读(1243)  评论(0编辑  收藏  举报