WHCSRL 技术网

Python编程:从入门到实践_动手试一试答案

动手试一试答案

第二章

#2-1 简单消息
message = "Hello Python world"
print(message)
  • 1
  • 2
  • 3
#2-2 多条简单消息
message = "Hello Python world"
print(message)
message = "Hello Python Crash Course world"
print(message)
  • 1
  • 2
  • 3
  • 4
  • 5
#2-3 个性化消息
name = "Eric"
print("“Hello "+name+", would you liketo learn some Python today?”")
  • 1
  • 2
  • 3
#2-4 调整名字的大小写
name = "Ada Lovelace"
print(name.title())
print(name.upper())
print(name.lower())
  • 1
  • 2
  • 3
  • 4
  • 5
#2-5 名言
print("Albert Einstein oncesaid,“Apersonwho never madea mistake never tried anything new.”")
  • 1
  • 2
#2-6 名言2
name = "Albert Einstein"
message= "“Apersonwho never madea mistake never tried anything new.”"
print(name + " once said, " + message)
  • 1
  • 2
  • 3
  • 4
#2-7 剔除人名中的空白
favorite_language = "
  Python   	"
print(favorite_language)
favorite_language.lstrip()
favorite_language.rstrip()
favorite_language.strip()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#2-8 数字8
print(5 + 3)
print(4 * 2)
print(8 / 1)
print(9 - 1)
  • 1
  • 2
  • 3
  • 4
  • 5
#2-9 最喜欢的数字
message = 888
print("My favorite number is " + str(message))
  • 1
  • 2
  • 3
#2-11 Python之禅
import this
  • 1
  • 2
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!

蒂姆·彼得斯的《Python禅》
美胜于丑。
显式比隐式好。
简单总比复杂好。
复杂总比复杂好。
平的比嵌套的好。
稀疏比密集好。
可读性很重要。
特殊情况不足以违反规则。
虽然实用胜过纯洁。
错误永远不应该悄无声息地过去。
除非明确沉默。
面对模棱两可的情况,拒绝猜测的诱惑。
应该有一个——最好只有一个——显而易见的方法来做到这一点。
虽然这种方式一开始可能并不明显,除非你是荷兰人。
现在总比没有好。
虽然永远都比现在好。
如果实现很难解释,那就不是个好主意。
如果实现很容易解释,这可能是一个好主意。
名称空间是一个非常好的主意——让我们做更多的工作吧!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

第三章

#3-1 姓名
names = ['trek', 'cannondale', 'redline', 'specialized']
for name in names:
    print(name)
  • 1
  • 2
  • 3
  • 4
#3-2 问候语
names = ['trek', 'cannondale', 'redline', 'specialized']
for name in names:
    print('Hello '+name+" good morning!")
  • 1
  • 2
  • 3
  • 4
#3-3 自己的列表
names = ['car', 'bicycle', 'bus']
for name in names:
    print('my favorite vehicle is ' + name + '.')
  • 1
  • 2
  • 3
  • 4
#3-4 嘉宾名单
persons = ["唐僧","孙悟空","猪八戒"]
for person in persons:
    print('您好 '+person+' ,我想请你吃饭!')
  • 1
  • 2
  • 3
  • 4
推荐阅读