python练习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python练习相关的知识,希望对你有一定的参考价值。

1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,
然后反序,在追加到文件的下一行中

list = [67, 234, 35, 14, 18, 6, 9, 5]
list.sort()
with open(‘20171030‘,‘w‘) as f:
f.write(str(list)+‘\n‘)
with open(‘20171030‘,‘r‘) as f:
a=f.read()
list.reverse()
with open(‘20171030‘,‘a‘) as f:
f.write(str(list))

2.分别把 string, list, tuple, dict写入到文件中

string = ‘awen‘
list=[1,2,3,4,5,6]
tuple=(1,2,3,4,5,6)
dict={‘q‘:1,‘a‘:2,‘z‘:3}
with open(‘20171030‘, ‘w‘) as f:
f.write(string+‘\n‘)
f.write(str(list)+‘\n‘)
f.write(str(tuple)+‘\n‘)
f.write(str(dict)+‘\n‘)

















以上是关于python练习的主要内容,如果未能解决你的问题,请参考以下文章

Python3练习题系列(03)

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

csharp Epicor标准练习片段

golang 去练习片段

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段