python语法学习五——函数库
程序员文章站
2022-07-10 08:09:59
...
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.ceil(3,6)
4
>>> math.degrees(78)
4469.070802020421
>>> math.radians(78)
1.361356816555577
os库
>>> import os
>>> os.getcwd()
'D:\\新建文件夹\\code\\C暑假训练\\python3.6'
>>> path='D:\\新建文件夹\\python练习'
>>> os.chdir(path)
>>> os.getcwd()
'D:\\新建文件夹\\python练习'
>>> os.rename('test2.py','test4.py')
>>> os.remove('test4.py')
random库
>>> import random
>>> random.choice(['c++','java'])
'c++'
>>> random.randint(1,100)
74
>>> random.randrange(1,100,2)
85
>>> random.random()
0.49685735561124855
>>> random.uniform(5,10)
8.208909979565709
>>> random.sample(range(100),10)
[98, 6, 65, 87, 24, 13, 75, 2, 94, 46]
>>> nums=[1001,1101,3432,4235,23525]
>>> random.shuffle(nums)
>>> nums
[1101, 4235, 1001, 3432, 23525]
datetime库
>>> import datetime
>>> dir(datetime)
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__', '__package__', '__spec__',
'_divide_and_round', 'date', 'datetime', 'datetime_CAPI', 'time',
'timedelta', 'timezone', 'tzinfo']
>>> from datetime import date
>>> date.today()
datetime.date(2020, 4, 1)
>>> from datetime import time
>>> tm=time(23,27,54)
>>> print(tm)
23:27:54
>>> from datetime import datetime
>>> dt=datetime.now()
>>> dt
datetime.datetime(2020, 4, 1, 22, 27, 51, 602412)
>>> print(dt.strftime('%a %b %d %Y %H:%M'))
Wed Apr 01 2020 22:27