Python入门习题大全——Python学习笔记

Python入门习题大全——索引

在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python知识,其中每一行都以“In Python youcan"打头。将这个文件命名为learning_python.txt。编写一个程序,它读取这个文件,并将你所写的内容打印三次:第一次打印时读取整个文件;第二次打印时遍历文件对象;第三次打印时将各行存储在一个列表中,再在with代码块外打印它们。

# Python 学习笔记
filename = 'learning_python.txt'

with open(filename) as file_object:
    contents = file_object.read()
    print(contents)

with open(filename) as file_object:
    for line in file_object:
        print(line.rstrip())

with open(filename) as file_object:
    lines = file_object.readlines()

print(lines)

learning = ''
for line in lines:
    learning += line

print(learning)

文件内容为:

In Python I can use any kind of num
In Python I can use letter with any operation
In Python I can use list, tuple, dictionary, for, while
In Python I can use function, class, file

输出为:

In Python I can use any kind of num
In Python I can use letter with any operation
In Python I can use list, tuple, dictionary, for, while
In Python I can use function, class, file
In Python I can use any kind of num
In Python I can use letter with any operation
In Python I can use list, tuple, dictionary, for, while
In Python I can use function, class, file
['In Python I can use any kind of num\n', 'In Python I can use letter with any operation\n', 'In Python I can use list, tuple, dictionary, for, while\n', 'In Python I can use function, class, file']
In Python I can use any kind of num
In Python I can use letter with any operation
In Python I can use list, tuple, dictionary, for, while
In Python I can use function, class, file
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伶回合

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值