val = -16
res = abs(val)
print(res)
"""奇进偶不进"""
val = 3.5
val = 4.5
val = 4.51
val = 4.12
val = 4.6
res = round(val)
print(res)
lst = [1,2,3,4,5]
res = sum(lst)
lst = [10,100,-3,40]
res1 = max(lst)
res2 = min(lst)
print(res1,res2)
lst_new = sorted(lst)
min_val = lst_new[0]
max_val = lst_new[-1]
print(min_val,max_val)
sorted(iterable,key=函数) max和min 同sorted用法:
'''找出年龄最小的元组'''
lst = [("王振",25),("刘伟",50),("刘思敏",18)]
def func(n):
# print(n)
return n[-1] # 返回的值是判断参考的依据
res = min(lst,key=func)
res = max(lst,key=func)
print(res)
dic = {"云超":100,"朱胜":200,"杨浩":-300}
def func(n):
# print(n)
# print(dic[n])
return abs(dic[n])
res = max(dic,key=func)
print(res)
res = pow(2,3) # 8
# 第三个参数的用作是取余
res = pow(2,3,5) # 3
print(res)
for i in range(1,11,3): # 1 4 7 10
print(i)
for i in range(10,0,-3):#`10 7 4 1
print(i)
res = bin(5) # 0b10
print(res)
res = oct(8) # 01234567 8 => 7+1 =>0o10
print(res)
res = hex(255)
res = hex(16) # 0x10
print(res)
"""大小字母相差的ascii编码是32.小写字母a => 97"""
res = chr(65)
print(res)
res = ord("A")
print(res)
strvar = "print(111)"
# strvar = "a = 10" eval执行不了
print(strvar)
eval(strvar)
strvar = "a = 10"
strvar = """
for i in range(10):
print(i)
exec(strvar)
# print(a)
strvar = "E:\nython30\tay15"
print(repr(strvar))
"""两个相同的字符串,无论哈希多少次,都会产生相同的唯一值"""
"""
(1) 让密码加密 hashlib
(2) 文件的校验 比较文件内容
"""
strvar1 = "abcccc"
strvar2 = "abcccc"
print(hash(strvar1))
print(hash(strvar2))
with open("ceshi1.txt",mode="r",encoding="utf-8") as fp:
strvar = fp.read()
res1 = hash(strvar)
with open("ceshi2.txt",mode="r",encoding="utf-8") as fp:
strvar = fp.read()
res2 = hash(strvar)
print(res1,res2)
序列化:
反序列化:
php:
在文件中存储的数据,要么是字符串,要么是字节流
python中,所有的数据类型都可以通过dumps和loads进行序列化和反序列化
lst = [1,2,3]
with open("ceshi2.txt",mode="a+",encoding="utf-8") as fp:
fp.write(lst)
lst = [1,2,3]
res = pickle.dumps(lst)
print(res)
def func():
print("我是func函数")
res = pickle.dumps(func)
print(res)
it = iter(range(10))
res = pickle.dumps(it)
res = pickle.loads(res)
print(res, type(res))
for i in range(3):
res2 = next(res)
print(res2)
dic = {"a":1,"b":2}
with open("ceshi3.txt",mode="wb") as fp:
# dump(要转换的数据,文件对象)
pickle.dump(dic,fp)
with open("ceshi3.txt",mode="rb") as fp:
res = pickle.load(fp)
print(res , type(res))
dic = {"a":1,"b":2}
str_bytes = pickle.dumps(dic)
print(str_bytes)
with open("ceshi4.txt",mode="wb") as fp:
fp.write(str_bytes)
# 通过loads来取出
with open("ceshi4.txt",mode="rb") as fp:
str_bytes = fp.read()
dic = pickle.loads(str_bytes)
print(dic,type(dic))
所有的编程语言都能够识别的数据格式叫做json,是字符串
能够通过json序列化成字符串的有如下类型:(int float bool str list tuple dict None)
pickle 序列化成字节流
json 序列化成字符串
引入模组:import json
dic = {"name":"王振","age":30,"classroom":"python30","family":["爸爸","妈妈","哥哥","姐姐"]}
# ensure_ascii=False 不通过ascii来显示内容 sort_keys=True 对字典的键来进行排序
res =json.dumps(dic,ensure_ascii=False , sort_keys=True)
print(res , type(res))
# loads反序列化成原来的数据类型
dic = json.loads(res)
print(dic , type(dic))
dic = {"name":"王振","age":30,"classroom":"python30","family":["爸爸","妈妈","哥哥","姐姐"]}
with open("ceshi5.json",mode="w",encoding="utf-8") as fp:
# dump(要转换的数据,文件对象)
json.dump(dic,fp,ensure_ascii=False)
# load反序列化成原来的数据类型
with open("ceshi5.json",mode="r",encoding="utf-8") as fp:
dic = json.load(fp)
print(dic, type(dic))
json 可以连续dump , 不可以连续load (load是一次性拿出所有数据进行反序列化,容易出错)
可以使用loads来解决
dic1 = {"a":1,"b":2}
dic2 = {"c":3,"d":4}
with open("ceshi6.json",mode="w",encoding="utf-8") as fp:
json.dump(dic1,fp)
fp.write("\n")
json.dump(dic2,fp)
fp.write("\n")
with open("ceshi6.json",mode="r",encoding="utf-8") as fp:
dic = json.load(fp)
print(dic)
解决方式,使用loads,一行一行进行反序列化
with open("ceshi6.json",mode="r",encoding="utf-8") as fp:
#文件对象是迭代器,一次迭代一行
for i in fp:
dic = json.loads(i)
print(dic)
import pickle
"""
pickle 可以连续dump , 可以连续load
"""
dic1 = {"a":1,"b":2}
dic2 = {"c":3,"d":4}
with open("ceshi7.pkl",mode="wb") as fp:
pickle.dump(dic1,fp)
pickle.dump(dic2,fp)
with open("ceshi7.pkl",mode="rb") as fp:
dic = pickle.load(fp)
print(dic)
dic = pickle.load(fp)
print(dic)
"""try .. except .. 抑制报错 如果try代码块里面有问题,就执行except中的代码"""
"""
try:
把有问题的代码放进来
except:
如果出现异常执行这个分支的代码块.
"""
try:
with open("ceshi7.pkl",mode="rb") as fp:
while True:
dic = pickle.load(fp)
print(dic)
except:
pass
json 和 pickle 两个模块的区别:
(1)json序列化之后的数据类型是str,所有编程语言都识别,(数据交流)
但是仅限于(int float bool)(str list tuple dict None)
json不能连续load,只能一次性拿出所有数据
(2)pickle序列化之后的数据类型是bytes, (存储转换)
所有数据类型都可转化,但仅限于python之间的存储传输.
pickle可以连续load,多套数据放到同一个文件中
print("[%-50s]" % ("###############"))
print("[%-50s]" % ("######################"))
print("[%-50s]" % ("##############################"))
strvar = ""
for i in range(50):
strvar += "#"
time.sleep(0.1)
print("\r[%-50s]" % (strvar) ,end="")
def progress(percent):
if percent > 1:
percent = 1
# 打印出对应的# 号效果
strvar = int(50 * percent) * "#"
# %% => %号的符号效果
print("\r[%-50s] %d%%" % (strvar , int(100 * percent)) ,end="")
recv_size = 0
total_size = 1028000
while recv_size < total_size:
time.sleep(0.01)
# 一次接受的字节数是1024
recv_size += 1024
percent = recv_size/total_size
progress(percent)
# 30% 50% 80%
# 0.3 0.8 0.9 1 100%