《Python编程从入门到实践》第二章练习题

2-1 简单消息: 将一条消息存储到变量中,再将其打印出来

>>> message = "Beautiful is better than ugly."
>>> print(message)
Beautiful is better than ugly.

2-2 多条简单消息: 将一条消息存储到变量中,将其打印出来;再将变量的值修改
为一条新消息,并将其打印出来

>>> message = "Explict is better than implicit."
>>> print(message)
Explict is better than implicit.
>>> message = "Simple is better than complicated."
>>> print(message)
Simple is better than complicated.

2-3 个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示
的消息应非常简单,如“Hello Eric, would you like to learn some Python today?”

>>> name = "Charlie"
>>> message = "How are you?"
>>> print(name + ", " + message)
Charlie, How are you?

2-4 调整名字的大小写: 将一个人名存储到一个变量中,再以小写、大写和首字母
大写的方式显示这个人名

>>> name = "CHarlie"
>>> print(name.lower())
charlie
>>> print(name.upper())
CHARLIE
>>> print(name.title())
Charlie

2-5 名言: 找一句你钦佩的名人说的名言,将这个名人的姓名和他的名言打印出
来。输出应类似于下面这样(包括引号):
Albert Einstein once said, “A person who never made a mistake never tried anything
new.”

>>> print("David Gelernter once said, \"Beauty is more important than anywhere else in technology because software is so complicated. Beraty is the ultimate defence against complexity.\"")
David Gelernter once said, "Beauty is more important than anywhere else in technology because software is so complicated. Beraty is the ultimate defence against complexity."

2-6 名言 2: 重复练习 2-5,但将名人的姓名存储在变量 famous_person 中,再创建
要显示的消息,并将其存储在变量 message 中,然后打印这条消息

>>> famouse_person = "David Gelernter"
>>> message = famouse_person + " once said, \"Beauty is more important than anywhere else in technology because software is so complicated. Beraty is the ultimate defence against complexity.\""
>>> print(message)
David Gelernter once said, "Beauty is more important than anywhere else in technology because software is so complicated. Beraty is the ultimate defence against complexity."

2-7 剔除人名中的空白: 存储一个人名,并在其开头和末尾都包含一些空白字符。
务必至少使用字符组合”\t”和”\n”各一次。
打印这个人名,以显示其开头和末尾的空白。然后,分别使用剔除函数 lstrip()、
rstrip()和 strip()对人名进行处理,并将结果打印出来

>>> name = "   Charlie\t\n  "
>>> print(name)
   Charlie  

>>> print(name.lstrip())
Charlie 

>>> print(name.rstrip())
   Charlie
>>> print(name.strip())
Charlie

2-8 数字 8: 编写 4 个表达式,它们分别使用加法、减法、乘法和除法运算,但结
果都是数字 8。为使用 print 语句来显示结果,务必将这些表达式用括号括起来,也就
是说,你应该编写 4 行类似于下面的代码:

print(5 + 3)

输出应为 4 行,其中每行都只包含数字 8

>>> print(5 + 3)
8
>>> print(9 - 1)
8
>>> print(2 * 4)
8
>>> print(24 // 3)
8

2-9 最喜欢的数字: 将你最喜欢的数字存储在一个变量中,再使用这个变量创建一
条消息,指出你最喜欢的数字,然后将这条消息打印出来

>>> number = 7
>>> print("My favorite number is " + str(number))
My favorite number is 7

2-10 添加注释: 选择你编写的两个程序,在每个程序中都至少添加一条注释。如
果程序太简单,实在没有什么需要说明的,就在程序文件开头加上你的姓名和当前日期,
再用一句话阐述程序的功能

'''
I'm cool
23333
'''
# again, I am cool
print("helloWorld")
helloWorld

2-11 Python 之禅: 在 Python 终端会话中执行命令 import this,并粗略地浏览一下
其他的指导原则

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

猜你喜欢

转载自blog.csdn.net/ill__world/article/details/79483260