pip命令行安装第三模块
安装 pip install 模块名
卸载 pip uninstall 模块名
查看所有安装 pip list
dir() 函数
内置的函数 dir() 可以找到模块内定义的所有名称。以一个字符串列表的形式返回:
import fibo, sys
dir(fibo)
['__name__', 'fib', 'fib2']
dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
'__package__', '__stderr__', '__stdin__', '__stdout__',
'_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
'_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
'call_tracing', 'callstats', 'copyright', 'displayhook',
'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
'thread_info', 'version', 'version_info', 'warnoptions']
如果没有给定参数,那么 dir() 函数会罗列出当前定义的所有名称:
a = [1, 2, 3, 4, 5]
import fibo
fib = fibo.fib
dir() # 得到一个当前模块中定义的属性列表
['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']
a = 5 # 建立一个新的变量 'a'
dir()
['__builtins__', '__doc__', '__name__', 'a', 'sys']
del a # 删除变量名a
dir()
['__builtins__', '__doc__', '__name__', 'sys']
random 随机数模块
定义:random() 方法返回随机生成的一个实数,它在[0,1)范围内。
语法:
import random
random.random()
实例
import random
# 生成第一个随机数
print "random() : ", random.random()
# 生成第二个随机数
print "random() : ", random.random()
以上实例运行后输出结果为
random() : 0.281954791393
random() : 0.309090465205
random()中常见函数如下:
import random
print(random.randint(1,43)) #随机一个整数,包含开始值和结束值
print(random.random()) #随机一个小数,0-1之间的小数
print(random.randrange(0,10,2)) #随即范围(含头含尾,不包含结束值)步长值
print(random.choice(["a","1","b","2","c","3"])) #随机序列中的某个值
print(random.randint(1000,9999)) #纯数字验证码
#大写字母+数字的n位验证码
def verification_code(n):
code = ""
for i in range(n):
num = random.randint(0,9)
if num % 2 == 0:
code += str(random.randint(0,9))
else:
code += chr(random.randint(65,90))
return code
print(verification_code(4))
time模块
import time
1.时间戳(小数) 1970-01-01 00:00:00(格林威治时间)
方便计算机储存,方便运算
print(time.time())
2.时间元组(命名元组类型)中间形态
#作为函数的参数,开发时使用
print(time.localtime())
3.格式化时间字符串
print(time.strftime("%d/%m/%Y %H:%M:%S"))
# 时间戳 -> 时间元组 -> 时间字符串
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(2131421413)))
时间字符串 -> 时间元组 -> 时间戳
print(time.mktime(time.strptime("2001-9-11 8:30:00","%Y-%m-%d %H:%M:%S")))
#睡眠
time.sleep(3)
返回当前系统时间
import datetime
datetime.datetime.now()
1.datetime日期输出格式化
datetime => string
now = datetime.datetime.now()
now.strftime('%Y-%m-%d %H:%M:%S')
strftime是datetime类的实例方法。
2.datetime日期字符串转为日期对象
string => datetime
t_str = '2019-03-05 16:26:23'
d = datetime.datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
strptime是datetime类的静态方法。