Python之list,string,tuple,dict练习题
#!/usr/bin/env python # -*- coding:utf8 -*- # @Time : 2017/10/30 10:11 # @Author : hantong # @File : threed_week.py import codecs #对列表排序 list = [22,4,6,8,10,334,779,234,54,666] list.sort() print(list) #写入列表到文件 with open("txt","w") as f: f.write('\n') f.write(str(list)) #追加反序到文件下一行 with open('txt','r') as f: t2 = f.read() print(t2) list.reverse() with open("txt",'a') as f: f.write('\n') f.write(str(list)) print(list) #分别把string,list,tuple,sict写入到文章中 string = 'akgnehkkd123' tuple = ('a','r','hhh','fe','hty') list = [23,'ff',456,'ghj','eryi'] dict = {'a':24,'d':56,'cd':89,'b':65} #将string写入到txt3文件中 with open('txt3','w') as f: f.write('\n') f.write(string) #将list写入到txt3文件中 with open('txt3','a') as f: f.write('\n') f.write(str(list)) #将tuple写入到txt3文件中 with open('txt3','a') as f: f.write('\n') f.write(str(tuple)) #将dict写入到txt3文件中 with open('txt3','a') as f: f.write('\n') f.write(str(dict))