1.python2和python3的区别
2.logging模块高级版
3.collections模块
4random模块
python2和python3的区别:
1.大环境:
python2:大神贡献源码,这些源码有自己语言的特色,这些源码不规范,源码重复太多.但是python崇尚的是优美,清晰,简单.
python3:龟叔重新整理,将源码规范化,简单化,统一化.
2.print()
python2:print("内容") print 内容
python3:print("内容")
3.input()
python2:raw_input("输出的是字符串") input(只能输出数字)
python3:input("输出的是字符串")
4.range()
python2:range()是列表
python3:range()可迭代对象
回顾logging低配版:
日志的信息,不能写入文件与显示同时进行.
1.被动触发:与异常处理配合.
2.主动触发:检测运维人员输入的指令,检测服务器的重要信息,访问记录等等.
import logging
logging.basicCinfig(level=logging.INFO,
format = "%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s",
filename = "low版logging.log")
msg = "CPU 正常,硬盘参数....,流量的max:...最小值...."
logging.info(msg)
高配版:
import logging
logger = logging.getLogger()#创建logger对象
fh = logging.FileHandler("高配版logging.log",encoding="utf-8")#创建文件句柄
sh = logging.StreamHandler()#产生一个屏幕句柄
format = logging.Formatter("%(asctime)s-%(name)s-%(levelname)s- %(message)s")#设置格式
logger.setLevel(logging.DEBUG)
#如果对logger对象设置日志等级,那么文件和屏幕都设置了
#总开关默认从warning开始,如果想设置分开关:必须要从他更高级:(ERROR,critical)从这个来开始设置
logger.addHandler(fh)#添加文件句柄
logger.addHandler(sh)#添加屏幕句柄
sh.setFormatter(formatter)#设置屏幕格式
fh.setFormatter(formatter)#设置文件的格式(这两个按照需求可以单独设置)
fh.setLevel(logging.DEBUG)
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")
collections 模块: 这个模块提供了一些特殊的数据类型.
namedtuple:
from collections import namedtuple
point = namedtuple("Point",["x","y"])
p = point(10,30) #相当于p = (x = 10,y = 30)
print(p.x)#10
print(p.y)#30
print(p[0])#10
print(p[1])#30
queue(对列):先进先出原则. fifo(first in first out)
栈:先进后出.
deque:双向队列
from collections import deque
q = deque(["a","b","c","d","e"])
print(q)
q.append(66)#添加最右边
q.append(77)
q.appendleft(111)#从最左边添加
q.appendleft(222)
q.pop()#从右边删除
q.popleft()#从左边删除
print(q)
OrderedDict(有序的字典):
dic = {}
dic["name"] = "alex"
dic["age"] = 1000
dic["sex"] = "男"
print(dic)
from collections import OrderedDict
od = OrderedDict()
od["name"] = "alex"
od["age"] = 1000
od["sex"]="男"
print(od)
#装逼版:
d = dict([("a",1),("b",2),("c",3)])
print(d)
from collections import OrderedDict
od1 = OrderedDict([("a",1),("b",2),("c",3)])
print(od1)
li = [11,22,33,44,55,77,88,90,99]
dic = {}
for i in li:
if i < 66:
if "key" not in dic:
dic["key"] = []
dic["key"].append(i)
else:
if "key1" not in dic:
dic["key1"] = []
dic["key1"].append(i)
print(dic)
from collections import defaultdict
li = [11,22,33,44,55,66,77,88,90,99]
my_dict = defaultdict(list)
my_dict["k1"]
my_dict["k2"]
print(my_dict)
my_dict = defaultdict(list)
for i in li:
if i < 66:
my_dict["k1''].append(i)
else:
my_dict["k2"].append(i)
print(my_dict)
dic = {}
for i in range(1,21):
dic[i] = 5
print(dic)
dic = {x:5 for x in range(1,21)}
dic = dict.fromkeys(range(1,21),5)
dic = defaultdict(lambda:5)
for i in range(1,21):
dic[i]
print(dic)
Counter(自动统计输入元素的个数)
from collections import Counter
c = Counter("adjfa;fsjdjlsjflksjfdljajce r")
print(c)
random(随机)
import random
print(random.random())#0~1之间的小数
print(random.uniform(1,3))#1~3之间的小数
print(random.randint(1,5))#1~5之间的整数,包括1和5
print(random.randrange(1,10,2))#1~10之间的奇数,不包括10,顾首不顾尾
print(random.choice(["a",2,[3,4,"b"],6]))#任选一个 常用
print(random.choice("djflajjdlajfk"))#字符串也可以,集合会报错,字典也会报错,
print(random.sample([2,3,2,4,5,6,3,2],4))#列表元素任意四个组合
item= [i for i in range(1,14)]
random.shuffle(item)#打乱顺序
print(item)