《Python编程:从入门到实践》 练习 10-1 10-2

不想写学校作业了,继续学习Python(加班)

10-1 Python学习笔记 :

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

with open('learning_python.txt') as file:
    learn = file.read()  #第一次没有加上.read(),导致结果出错,要专心输入
    print(learn)

with open('learning_python.txt') as file:
    for line in file:
        print(line.rstrip())

with open('learning_python.txt') as file:
    lines = file.readlines()  #此处readlines少打了一个s,输出了一行一个字母的结果,要专心输入

for line in lines:
    print(line.rstrip())

运行结果:

In Python you can use less code than Java and C.
In Python you can find out if your birthday is included in the PI.
In Python you can use less code than Java and C.
In Python you can find out if your birthday is included in the PI.
In Python you can use less code than Java and C.
In Python you can find out if your birthday is included in the PI.

10-2 C语言学习笔记 :

可使用方法replace() 将字符串中的特定单词都替换为另一个单词。
下面是一个简单的示例, 演示了如何将句子中的’dog’ 替换为’cat’:
>>> message = "I really like dogs."
>>> message.replace(‘dog’, ‘cat’)
’I really like cats.'
读取你刚创建的文件learning_python.txt中的每一行, 将其中的Python都替换为另一门语言的名称, 如C。
将修改后的各行都打印到屏幕上。

with open('learning_python.txt') as file:
    learn = file.read()
    print(learn.replace('Python', 'PHP')) #此处漏写''导致结果出错,要专心输入

运行结果:

In PHP you can use less code than Java and C.
In PHP you can find out if your birthday is included in the PI.

反思与总结

学习的时候要专心,切忌浮躁(即使室友很吵,这说明抗干扰能力不够)
罚写3遍专心学习:
专心学习 专心学习 专心学习

你可能感兴趣的:(Python(暂时放弃))