模块collections,time,random,sys模块

一、collections模块

# from collections import OrderedDict
# d = OrderedDict()
# d['a'] = 1
# d['z'] = 2
# d['b'] = 3
# print(d)
#
# d['z'] = 0
# print(d)

# {'apple':100,'computer':10000}

# 默认字典
# 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
# 将所有大于 66 的值保存至字典的第一个key中,
# 将小于 66 的值保存至第二个key的值中。
# dic = {}
# l = [11,22,33,44,55,66,77,88,99,90]
# for i in l:
# if i>66:
# if dic.get('k1'):
# dic['k1'].append(i)
# else:
# dic['k1'] = [i]
# elif i<66:
# if dic.get('k2'):
# dic['k2'].append(i)
# else:
# dic['k2'] = [i]
# print(dic)

from collections import defaultdict

# d = defaultdict(set)
# print(d)
# print(d['a'])
# d['b'] = 10
# print(d)


# defaultdict(list) # callable
# l = []
# dic = {'k1':l,'k2':l}

# d = defaultdict(list) #{1:''}
# print(d)
# print(d[1])
# print(d)

# {} 默认的value是5
# from collections import defaultdict
# d = defaultdict(lambda : 5)
# print(d[1])
# print(d)

# from collections import defaultdict
# d = defaultdict(list)
# l = [11,22,33,44,55,66,77,88,99,90]
# for i in l:
# if i>66:
# d['k1'].append(i)
# elif i<66:
# d['k2'].append(i)
#
# print(d)

# 默认字典最大的好处就是 永远不会在你使用key获取值的时候报错
# 默认字典 是给 字典中的value设置默认值

# from collections import Counter
# c = Counter('abcdeabcdabcaba')
# print(c)

# class Configparser:
# def __init__(self,section,option):
# self.section = section
# self.option = option
# def write(self,f):
# f.write(self.section,self.option)
#
#
# f = open('test','w')
# config = Configparser('a','b')
# config.write(f)
#
#
# import configparser
# configparser.ConfigParser.write()

二、time模块
import time
# 时间
# 计算执行代码的时间 time.time() # 1524536396.9483523
# 让程序停在这里一段时间 sleep

# 记录时间的格式:
# 给人看的
# 给机器看的
# 计算用的

import time
# 时间戳时间
# print(time.time()) # 时间戳时间 # 计算时间差 精准的运算
#格式化时间
# print(time.strftime('%Y-%m-%d %H:%M:%S'))# 字符串格式化时间 # 记录下来给人看
# print(time.strftime('%y-%m-%d'))# 字符串格式化时间
# print(time.strftime('%x %X'))# 字符串格式化时间
# print(time.strftime('%c'))# 字符串格式化时间
#结构化时间
# print(time.localtime())# 本地结构化时间 # 时间戳时间转格式化时间的中间件
# 对应项的简单计算
# print(time.gmtime()) # 英国的结构化时间

# 2015-8-8
# p = time.strptime('2015-8-8','%Y-%m-%d')
# print(p)
# print(time.mktime(p))
# print(time.time()-time.mktime(p))

# print(time.time())
#
# ret = time.localtime(1500000000)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret))

# ret = time.localtime(2000000000)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret))

# ret = time.localtime(3000000000)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) #2033 2065 32

# ret = time.localtime(0)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) #1970 1 1 8
#
# ret = time.gmtime(0)
# print(ret)
# print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) #1970 1 1 0


# print(time.strftime('%c'))
# print(time.ctime(1500000000))

# ret = time.localtime(2000000000)
# print(ret)
# print(time.asctime())
# print(time.asctime(ret))

# 作业
# y-m-d h:M:S
# 从当前时间开始 比起y-m-d h:M:S过去了多少年 多少月 多少天 多少h,多少m,多少s

三、random模块
import random
# 随机 打乱顺序有关的

# print(random.random()) # 0-1随机小数
# print(random.uniform(1,4))

# print(random.randint(1,1000)) # 包含1,2
# print(random.randrange(1,2)) # 不包含2
# print(random.randrange(1,20,2)) # 包含所有奇数

# print(random.choice([1,'23',[4,5]])) #随机取一个元素

# print(random.sample([1,'23',[4,5]],2)) #随机取两个元素

# item=[1,3,5,7,9]
# import random
# random.shuffle(item) #打乱排序
# print(item)

# 要求 生成随机验证码
# 基础需求: 6位数字验证码 数字可以重复
# 进阶需求: 字母+数字 4位验证码 数字字母都可以重复

四、sys模块
# sys.argv           命令行参数List,第一个元素是程序本身路径
# sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit(1)
# sys.version 获取Python解释程序的版本信息
# sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
# sys.platform 返回操作系统平台名称

# import sys
# print('*'*6)
# sys.exit() #退出程序,正常退出时exit(0),错误退出sys.exit(1)
# print('-'*6)

# import sys
# print(sys.version)

# import sys
# print(sys.platform)

# import sys # *****
# print(sys.path)

# 内存
# 程序在运行起来的时候
# 启动解释器 加载一些基础的内容 内置函数 内置模块 -->内存里
# sys.path 系统的路径 import
# import os,sys

# import sys
# print(sys.argv) # 列表 列表的第一项是当前文件所在的路径
# if sys.argv[1] == 'alex' and sys.argv[2] == '3714':
# print('登陆成功')
# else:
# sys.exit()
# user = input('>>>')
# pwd = input('>>>')
# if user == 'alex' and pwd == '3714':
# print('登陆成功')
# else:
# sys.exit()
# print('我能完成的功能')


# 执行一个程序
# debug
# 直接执行
import sys
import logging
inp = sys.argv[1] if len(sys.argv)>1 else 'WARNING'
logging.basicConfig(level=getattr(logging,inp)) # DEBUG
num = int(input('>>>'))
logging.debug(num)
a = num * 100
logging.debug(a)
b = a - 10
logging.debug(b)
c = b + 5
print(c) # 3000

原文地址:https://www.cnblogs.com/zzw731862651/p/8933330.html