Python练习作业

# Author:xjy
# coding:utf-8
# 1、列表与元组的区别是什么?
# 1、列表list:有序,可变。
# 2、元组tuple:有序,不可变。
# 3、字典dict:无序,字典的key是不可变的,值是可变的

# 2、结合具体的代码案例说明演示字典的有排和字典的排序

dict2={'name':'qqq','age':'20','sex':'M','address':'xian'}

data1=dict(sorted(dict2.items(),key=lambda s:s[0]))
print(data1)
data1=dict(sorted(dict2.items(),key=lambda s:s[1]))
print(data1)
print(dict2.items())

 

 


# 3使用列表推导式实现取出100数字以内整除2的数字
list1=[x for x in range(1,101) if x%2==0]
print(list1)

 

 


#
# 4、在如下数据中获取学生成绩,输出如下几点:
# A、学生最大,最小,平均成绩
# B、过滤出成绩大于等于60的学生成绩
# C、过滤出成绩小于60分的

dict1={'status':0,'msg':'msg','data':[{'name':"lisi","score":"90"},{'name':"wangwu","score":"60"},
{'name':"lisi1","score":"88"},
{'name':"lisi2","score":"59"},
{'name':"lisi3","score":"24"},
{'name': "lisi4", "score": "66"},
{'name': "lisi5", "score": "70"},
{'name': "lisi6", "score": "45"},
{'name': "lisi7", "score": "39"}
]}

list2=[]
for item in dict1['data']:
list2.append(int(item['score']))
print(list2)
print(max(list2)) #最高分
print(min(list2)) #最低分
print(sum(list2)/len(list2)) #平均分
list3=[x for x in list2 if x>=60] #大于等于60
print(list3)
list4=[x for x in list2 if x<60] #小于60
print(list4)

 

 



# 5、结合函数的返回值编写一个案例
def login():
username=input('请输入账户')
password=input('请输入密码')
if username=='admin' and password=='admin':
return True
else:
return '你的账户号密码不正确'
print(login())

 

 



# 6、什么是序列化与反序列化,结合字典编写,结合文件实现
import json

json.dump(dict1,open('json.txt','w',encoding='utf-8'))
data=json.load(open('json.txt'))
print(data,type(data))

 

 



# 7、获取当前时间
import datetime
print(datetime.datetime.now())

 

 


# 8、编写一个读取文件内容和把内容写入文件的程序
def write():
with open('exe.txt','w',encoding='utf-8')as w:
w.write(input('请输入内容\n'))

def read():
with open('exe.txt', 'r') as f:
lists=f.read()
print(lists)

def main():
while True:
try:
f=int(input('1、写入 2、读取 \n' ))
if f==1:
write()
elif f==2:
read()
else:
break
except:continue

if __name__ == '__main__':
main()

 

posted @ 2022-06-23 19:28  饭依然特稀  阅读(24)  评论(0编辑  收藏  举报