高级特性
1.切片
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
L[0:3]
#['Michael', 'Sarah', 'Tracy'] #顺序切片
L[-2:0]
# ['Bob', 'Jack'] #倒数切片
L[0:10:2] #前十个每隔两个取一个
IO操作
Output
with open('D:/1.txt','r') as f:
for line in f.readlines():
print(line.strip()) # 把末尾的'\n'删掉
#读取一行,输出一行
Input
with open('D:/1.txt', 'w') as f:
f.write('Hello, world!')
#写入‘Hello,world!’
w会将文件重写,若要追加可以使用a(append)
网友评论